Skip to content

Instantly share code, notes, and snippets.

@sunilnandihalli
Created November 25, 2010 07:44
Show Gist options
  • Save sunilnandihalli/715047 to your computer and use it in GitHub Desktop.
Save sunilnandihalli/715047 to your computer and use it in GitHub Desktop.
a macro to help in debugging code in let form...
(ns letd
(:require [clojure.contrib.macro-utils :as m]))
(defmacro with-seperator [& body]
(let [line '(println (apply str (repeat 61 "-")))]
`(m/symbol-macrolet [sep ~line]
(m/with-symbol-macros
(do sep (let [x# (do ~@body)] sep x#))))))
(defmacro display-local-bindings []
`(do ~@(map (fn [x#] (list 'println [`'~x# x#])) (keys &env))))
(defmacro letd [ bindings & body]
(let [lbinds (take-nth 2 bindings)
rbinds (take-nth 2 (drop 1 bindings))
lsyms (repeatedly (count lbinds) gensym)]
; `(with-seperator
`(do
(let ~(vec (concat (interleave lsyms rbinds)
(interleave lbinds lsyms)))
(println ~@(map vector
(map #(do (list 'quote %)) lbinds)
(map #(do `(quote ~%)) rbinds)
lsyms))
(display-local-bindings)
~@body))))
#_(letd [x 10]
x)
#_(letd [x {:a 1 :b 2}
y (take-nth 2 (range 10))]
[x y])
#_(letd [x {:a 1 :b 2}
y (take-nth 2 (range 10))]
[x y])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment