Since the OP has requested other possible ways of writing this macro (see comments on the accepted answer), here it is:
(defmacro exec-all [& commands] `(doseq [c# ~(vec (map (fn [c] `(fn [] (println "Code: " '~c "=> Result: " ~c))) commands))] (c#)))
It will expand to about
(doseq [c [(fn [] (println "Code: " '(conj [2 3 4] 5) "=> Result: " (conj [2 3 4] 5))) (fn [] (println "Code: " '(+ 1 2) "=> Result: " (+ 1 2)))]] (c))
Note that the forms fn , whose values ββwill be attached to c , are collected in the vector at the macro expansion time.
Needless to say, the original version is simpler, so I think (do ...) is the perfect solution. :-)
Interaction Example:
user=> (exec-all (conj [2 3 4] 5) (+ 1 2)) Code: (conj [2 3 4] 5) => Result: [2 3 4 5] Code: (+ 1 2) => Result: 3 nil
MichaΕ Marczyk
source share