How to pass a list to clojure's `->` macro? -


i'm trying find way thread value through list of functions.

firstly, had usual ring-based code:

(defn make-handler [routes]   (-> routes       (wrap-json-body)       (wrap-cors)       ;; , on       )) 

but not optimal wanted write test check routes wrapped wrap-cors. decided extract wrappers def. code became follows:

(def middleware   (list ('wrap-json-body)         ('wrap-cors)         ;; , on         ))  (defn make-handler [routes]   (-> routes middleware)) 

this apparently doesn't work , not supposed -> macro doesn't take list second argument. tried use apply function resolve that:

(defn make-handler [routes]   (apply -> routes middleware)) 

which bailed out with:

compilerexception java.lang.runtimeexception: can't take value of macro: #'clojure.core/->

so question arises: how 1 pass list of values -> macro (or, say, other macro) 1 apply function?

you can make macro that:

;; notice better use quote, qoute function names macro, qualifies them. (def middleware   `((wrap-json-body)     (wrap-cors))     ;; , on    )  (defmacro with-middleware [routes]   `(-> ~routes ~@middleware)) 

for example this:

(with-middleware [1 2 3]) 

would expand this:

(-> [1 2 3] (wrap-json-body) (wrap-cors)) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -