(defmacro def+
"def with binding (def+ [{:keys [a b d]} {:a 1 :b 2 :d 3}])"
[bindings]
(let [let-expr (macroexpand `(let ~bindings))
vars (filter #(not (.contains (str %) "__"))
(map first (partition 2 (second let-expr))))
def-vars (map (fn [v] `(def ~v ~v)) vars)]
(concat let-expr def-vars)))
;Example usage:
(def+ [a 1 b 2])
(def+ [{:keys [a b]} {:a 1 :b 2}])
(def+ [z 1
{:keys [a b]} {:a 1 :b 2}])
This should not be used in the final code - though it will work - but probably using in REPL is a better use for this macro.
Genius! This is bloody genius!
ReplyDeleteNarayan, I loved your macro so much that I wrote an essay about it:
ReplyDeletehttp://www.learningclojure.com/2010/09/astonishing-macro-of-narayan-singhal.html
I hope you don't mind. If you do, I'll take it down, and you must feel free to steal anything you like back in compensation.
This is awesome! Thank you.
ReplyDelete