Last active
April 21, 2021 01:42
-
-
Save mikeananev/346cd6084d98381e7cf1378ef423a56a to your computer and use it in GitHub Desktop.
debug with clojure tap>
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
;; 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