Wednesday, April 1, 2009

Destructuring binding in def

A lot of times when I am debugging a clojure function in REPL. I end up doing defs for all my lets and if my let is using the destructuring bind than it become a little tedious process. The following macro helps me substitute the  let with def+ to define the symbols in my REPL:

(defmacro def+
  "def with binding (def+ [{:keys [a b d]} {:a 1 :b 2 :d 3}])"
  (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.


  1. Narayan, I loved your macro so much that I wrote an essay about it:

    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.