I believe maps containing functions are generally looked down upon. Multimethods are probably the way to go. Can't you tag your request map with a type? On Thu, Sep 17, 2009 at 7:50 AM, Philipp Meier <phme...@gmail.com> wrote:
> > Hi, > > I'm building a REST server library on top of compojure loosely modeled > after the ideas of erlangs webmachine. The idea is to describe a > resource using a couple of function which server as decision makers > for the different stages of HTTP request processing. There will be a > function to determine if the request method is allowed, if the request > is authorized, a method to produce an ETAG or to process the request > body in case of a POST request. Something like > > (defn valid-method? [request] (= (get-method request) :get) > (defn generate-etag [request] (make-hash-md5 (get-path request))) > (defn process-post [request] (str "The body was" (get-body request)) > > The question is: how can on group the methods together for different > resources and to enable fall back do default implementation of the > methods (e.g. generate an etag by sha-ing the response body)? > > Two solutions come to my mind: multimethods and maps of methods. > > For multimethods id declare a type :rest-ressource which methods will > serve as a default implementation and that could be overridden by each > sub type resource. > > By maps of methods I mean something like > > (def rest-default-impl { > :valid-method? (fn [request] (contains [:head :get] (get-method > request))) > # ... > }) > > (defn make-rest [method-map] > (merge rest-default-impl method-map)) > > (def test-resource make-rest({ :valid-method? (fn [request] (= (get- > method request) :get)) > :body { :text/html (fn > [request] "Hello, world.") }})) > > > > Which way would you prefer? What are to advantaces and drawbacks of > each? The method-map approach seams more fp to me, but, I think that's > like how multimethods are impemented, aren't they? > > -billy. > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---