Skip to content

Instantly share code, notes, and snippets.

@mkremins
Created September 9, 2015 16:27
Show Gist options
  • Save mkremins/0578e07a37ebefa69659 to your computer and use it in GitHub Desktop.
Save mkremins/0578e07a37ebefa69659 to your computer and use it in GitHub Desktop.
Like clojure.core/let, but logs the value of every bound symbol
(defn indent [n s]
(let [spaces (clojure.string/join (repeat n " "))]
(->> (clojure.string/split-lines s)
(map (partial str spaces))
(clojure.string/join "\n"))))
(defn bound-syms [pat]
(condp #(%1 %2) pat
(some-fn map? vector?) (distinct (mapcat bound-syms pat))
symbol? [pat]
[]))
(defn log-bindings [pat]
(letfn [(log-binding [sym]
`[(println ~(name sym))
(println (indent 2 (with-out-str (clojure.pprint/pprint ~sym))))])]
`(do ~@(mapcat log-binding (bound-syms pat)))))
(defmacro let-log [bvec & body]
`(let ~(reduce (fn [bvec [pat val]]
(into bvec [pat val '_ (log-bindings pat)]))
[] (partition 2 bvec))
~@body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment