Skip to content

Instantly share code, notes, and snippets.

@mikeananev
Last active April 21, 2021 01:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeananev/346cd6084d98381e7cf1378ef423a56a to your computer and use it in GitHub Desktop.
Save mikeananev/346cd6084d98381e7cf1378ef423a56a to your computer and use it in GitHub Desktop.
debug with clojure tap>
;; order for printing values
(def prn-lock (Object.))
(defmacro dbg*
"Macro sends form to tap> and returns the same form"
[args]
`(let [args# ~args]
(tap> (sorted-map :form (quote ~args) :meta (assoc ~(meta &form) :ns ~(str *ns*)) :ret args#))
args#))
(defonce tap-prn-f (bound-fn* (fn [v] (locking prn-lock (prn v)))))
(defmacro debug-> [& fns]
`(-> ~@(interleave fns (repeat 'dbg*))))
(defmacro debug->> [& fns]
`(->> ~@(interleave fns (repeat 'dbg*))))
(defn prn-tap
"add prn function to catch tap> values"
[]
(add-tap tap-prn-f))
(defn prn-untap
"remove prn function from catching tap values"
[]
(remove-tap tap-prn-f))
(comment
(prn-tap)
(dbg* (+ 2 3)) ;; => 5
;; =>{:form (+ 2 3), :meta {:line 45, :column 3, :clojure.core/eval-file "/Users/mike/projects/app01/src/my/example/app01/debug.clj", :ns "my.example.app01.debug"}, :ret 5}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment