macros - How to use defmacro instead of eval? -
i have come below function, works intended uses eval horrible, , not exist in clojurescript intend use it.
(defn path [d p] (eval (concat '[-> d] (flatten (map #(conj (repeat (dec %) 'z/right) 'z/down) (path-to-vector p))))))
how convert macro? attempt looks this:
(defmacro path [d p] `(concat (-> ~d) (flatten (map #(conj (repeat (dec %) z/right) z/down) (path-to-vector ~p)))))
but not work.
no need macro or eval, operation reduce
:
(defn path [d p] (reduce (fn [s v] (reduce #(%2 %1) s (conj (repeat (dec v) z/right) z/down))) d (path-to-vector p)))
also note (conj (repeat (dec %) z/right) z/down)
means z/down , z/right coz repeate return sequence, in case want z/right first , last item should z/down should use (conj (vec (repeat (dec %)) z/right) z/down)
Comments
Post a Comment