Hi Rowdy, Am 18.07.2009 um 20:35 schrieb Rowdy Rednose:
your negative answer actually helped me analyze my problem better and get the distinction run time / expansion time straight. I figured that the names of the bindings are already there at expansion time and it's only the values that I need to retrieve at run time.
Good to hear, you found a solution for your problem.
(defmacro let-coll [ks val-fn & body] `(let ~(vec (mapcat #(list % `(~val-fn '~%)) ks)) ~...@body))
Another idea might be to translate the symbols for the
variables to keywords in the map. That would save
you the tedious quoting everywhere. Similar to the :keys
destructuring of let.
Although you evaluate ~val-fn several times, which might
be a problem with (make-value-fn-via-long-computation)...
(defmacro let-coll
[ks val-fn & body]
(let [val-fn-g (gensym "val-fn__")]
`(let ~(reduce #(-> %1
(conj %2)
(conj (list val-fn-g (keyword (name %2)))))
[val-fn-g val-fn] ks)
~...@body)))
And here in action:
user=> (macroexpand-1 '(let-coll [a b c] {:a 1 :b 2 :c 3} (println a b
c)))
(clojure.core/let [val-fn__23 {:a 1, :b 2, :c 3}
a (val-fn__23 :a)
b (val-fn__23 :b)
c (val-fn__23 :c)]
(println a b c))
user=> (let-coll [a b c] {:a 1 :b 2 :c 3} (println a b c))
1 2 3
I actually like this idea. :)
Sincerely
Meikel
smime.p7s
Description: S/MIME cryptographic signature
