Monday, August 01, 2005

Syntactic Abstraction

One of Lisp's primary attractions for me is the ability to build syntactic abstractions. Too many languages require you to write the same N lines of code every time you want to accomplish a common task, but with Lisp you can write a function or macro to abstract it. Consider the following code which implements the REST API's place/find-near-point function:

(def-api-function place/find-near-point
((key 'string "user access key")
(latitude 'float "latitude of center point")
(longitude 'float "longitude of center point")
(distance 'integer "distance in meters"))
"Find places within the radius of a circle centered on the
specified latitude and longitude."
(let ((places (if (and latitude longitude distance)
(find-places-near (list latitude longitude) distance)
(list (make-dummy-place)))))
(with-api-response (stream)
(dolist (place places)
(generate-place-details stream place)))))

Isn't that pretty? Concise and simple, yet it accomplishes quite a number of repetitive tasks including registering the function as an API request handler, decoding and validating input parameters, and generating either a help page or the appropriate XML response.

2 Comments:

Anonymous Anonymous said...

AIUI, you are doing two things: you are proving a REST API for your data, and you are dynamically generating HTML pages from your data.

Are you internally querying your data via the REST API as well, or is this handled with another function, or even through def-api-function magic (if so, how do you discriminate the caller)?

1:30 AM  
Blogger CafeSpot said...

The HTML pages and the REST API use the same backend functions to grab data, in this case find-places-near.

1:17 PM  

<< Home