Hi,

Recently I found myself wanting a macro similar to some-> or some->> but one 
where I could specify an arbitrary predicate instead of nil?. For example, I 
have a Ring request map which I wish to pass through a number of functions. Any 
of these functions might return an error response and if so I do not want to 
evaluate any remaining forms--much like how some-> will not evaluate after a 
nil response. To do this, I wrote a macro that is essentially some-> but which 
takes a predicate.

Here's the macro I ended up with:

(defmacro until-pred->
  "Like some-> but evalutes via -> until or if a predicate is true."
  [pred expr & forms]
  (let [g     (gensym)
        pstep (fn [step]
                `(if (~pred ~g)
                   ~g
                   (-> ~g ~step)))]
    `(let [~g ~expr
           ~@(interleave (repeat g) (map pstep forms))]
       ~g)))

An example of how I might use this:

(defn my-handler
  [request]
  (until-pred-> error-response? request
                check-auth
                check-balance
                ...))

That said, I'm wondering if there is a more idiomatic way of passing Ring 
request maps through a series of functions, each of which might be a terminal 
step? I'm also curious if there's a particular reason some-> wasn't implemented 
in a more general way; perhaps the fact that I can so easily write a macro that 
achieves this myself is a good enough reason?

Thanks,


Max

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to