Created
September 9, 2015 16:27
-
-
Save mkremins/0578e07a37ebefa69659 to your computer and use it in GitHub Desktop.
Like clojure.core/let, but logs the value of every bound symbol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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