Created
July 23, 2013 11:41
-
-
Save leobm/6061734 to your computer and use it in GitHub Desktop.
break point in clojure - A macro from 《the joy of clojure》 for debugging:
This file contains 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 contextual-eval [ctx expr] | |
(eval | |
`(let [~@(mapcat (fn [[k v]] [k `'~v]) ctx)] | |
~expr))) | |
(defmacro local-context [] | |
(let [symbols (keys &env)] | |
(zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols))) | |
(defn readr [prompt exit-code] | |
(let [input (clojure.main/repl-read prompt exit-code)] | |
(if (= input ::tl) | |
exit-code | |
input))) | |
;;make a break point | |
(defmacro break [] | |
`(clojure.main/repl | |
:prompt #(print "debug=> ") | |
:read readr | |
:eval (partial contextual-eval (local-context)))) |
This file contains 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
;; Usage: | |
(defn div [n d] (break) (int (/ n d))) | |
(div 10 0) | |
debug=> n | |
10 | |
debug=> d | |
0 | |
debug=> (local-context) | |
{n 10, d 0} | |
debug=> ::tl | |
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:156) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment