clojure - Simple macro doesn't seem to execute given forms -


i'm using basic macro building block other timing macros:

(defmacro time-pure   "evaluates expr , returns time took.   modified native time macro return time taken."   [expr]   `(let [start# (current-nano-timestamp)          ret# ~expr]      (/ (double (- (current-nano-timestamp) start#)) 1000000.0))) 

i've tested this, , used in other macros, know works fine.

my problem can described following snippet:

(defmacro time-each [& exprs]   `(mapv #(time-pure %) '~exprs)) 

i expect give each expression time-each, executes , times it; returning result. when test however, finishes instantly:

(time-each   (thread/sleep 500)   (thread/sleep 1000))  [0.036571 0.0] 

i'm confused since know (time-pure (thread/sleep 1000)) take around second return, , macro delegate time-pure.

what's causing this? have no idea how debug macro. used macro-expand-1 inspect generated code:

(clojure.pprint/pprint   (macroexpand-1     '(time-each       (thread/sleep 500)       (thread/sleep 1000))))  (clojure.core/mapv  (fn*   [p1__1451__1452__auto__]   (helpers.general-helpers/time-pure p1__1451__1452__auto__))  '((thread/sleep 500) (thread/sleep 1000))) 

but nothing stands out me.

what's going on here?

(note, dupe of question posted few minutes ago. realized case showing convoluted, made better example.)

(defmacro time-each [& exprs]   `(list ~@(for [expr exprs]              `(time-pure ~expr)))) 

you need careful not evaluate expression outside of time-pure context, done in arthur's answer, evaluates each expression , calls time-pure on (very fast) operation of "looking @ result". rather, wrap time-pure around each expression before evaluating it.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -