Miikka Miettinen <[email protected]> writes:
Hi Miikka,
> I sometimes find myself wanting to write simple conditional
> expressions inside -> and ->>. For example, it would be nice to create
> Ring responses like this:
>
> (-> {}
> (status code)
> (content-type "application/json;charset=utf-8")
> (when (= error-type :unauthorized)
> (header "WWW-Authenticate" "Basic realm=\"my-realm\""))
> (when (not= request-method :head)
> (assoc :body (json-str ...))))
>
> The above obviously doesn't work, but is there some other macro
> somewhere that would enable me to do this?
You can embed conditions in thread-through expressions, you just have to
make sure that 1. the previous result will be spliced in, and 2. your
conditional form has to return a result suitable for the next form in
any case, thus you probably want to use `if' instead of `when'.
I don't know Ring, so here's an contrieved example.
user> (-> 1
inc
(#(if (> % 2) (- %) %))
inc)
3
user> (-> 2
inc
(#(if (> % 2) (- %) %))
inc)
-2
Bye,
Tassilo
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en