Skip to content

Instantly share code, notes, and snippets.

@sunilnandihalli
Created November 28, 2010 08:30
Show Gist options
  • Save sunilnandihalli/718725 to your computer and use it in GitHub Desktop.
Save sunilnandihalli/718725 to your computer and use it in GitHub Desktop.
a few macros to help in debugging
(defmacro print-and-return
([x]
`(let [x# ~x]
(println x#) x#))
([flag x]
`(let [flag# '~flag
x# ~x]
(println flag#)
(println x#)
x#)))
(defmacro with-separator [& body]
`(do
(println)
(println (apply str (repeat 61 "-")))
(let [x# (do ~@body)]
(println (apply str (repeat 61 "-")))
x#)))
(defmacro display-local-bindings []
"displays all the non-var bindings visible in the current local scope. It relies on the &env that is
automatically passed to all the macro expansions"
`(do ~@(map (fn [x#] (list 'println [`'~x# x#])) (keys &env))))
(defmacro letd [bindings & body]
"it tries to print the values of all the bindings as and when they occur with out waiting for all
bindings to execute.. could be very helpfull when one of the other bindings fails or throws and
exception. all you have to do to use this is just add d to your let form .. otherwise you don't have
to change anything else in your let form"
(let [lbinds (take-nth 2 bindings)
rbinds (take-nth 2 (drop 1 bindings))
lsyms (repeatedly (count lbinds) gensym)]
(reduce (fn [val [lsym lbind rbind]] `(with-separator
(let [~lsym ~rbind
~lbind ~lsym]
(display-local-bindings)
(clojure.pprint/pprint ['~lbind
'~rbind
~lsym])
~val)))
`(do ~@body)
(reverse (map vector lsyms lbinds rbinds)))))
(defmacro ->d [ & args]
"to see the values that are getting passed during the chaining .. it gets printed immediately without
waiting for the rest of the chain to execute. Could be usefull in case one of the following functions
throws an exception."
`(with-separator (-> ~@(interpose 'print-and-return args) print-and-return)))
(defmacro ->dd [ & args]
"to see the values that are getting passed during the chaining .. it gets printed immediately without
waiting for the rest of the chain to execute. Could be usefull in case one of the following functions
throws an exception."
`(with-separator (-> ~@(interleave args
(map (fn [x] `((fn [k#] (print-and-return ~x k#))))
args)))))
(defmacro ->>d [ & args]
"to see the values that are getting passed during the chaining .. it gets printed immediately without
waiting for the rest of the chain to execute. Could be usefull in case one of the following functions
throws an exception."
`(with-separator (->> ~@(interpose 'print-and-return args) print-and-return)))
(defmacro ->>dd [& args]
"to see the values that are getting passed during the chaining .. it gets printed immediately without
waiting for the rest of the chain to execute. Could be usefull in case one of the following functions
throws an exception."
`(with-separator (->> ~@(interleave args
(map (fn [x] (list 'print-and-return x)) args)))))
(defn scaffold [iface]
"this code is from Christophe Grand .. but very usefull.. so I chose to include.."
(doseq [[iface methods] (->> iface .getMethods
(map #(vector (.getName (.getDeclaringClass %))
(symbol (.getName %))
(count (.getParameterTypes %))))
(group-by first))]
(println (str " " iface))
(doseq [[_ name argcount] methods]
(println
(str " "
(list name (into ['this] (take argcount (repeatedly gensym)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment