Last active
December 12, 2015 04:18
-
-
Save duckyuck/4713063 to your computer and use it in GitHub Desktop.
Debug macro
This file contains hidden or 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
(ns me.andersfurseth.debug | |
(:use [clojure.walk :only [postwalk]])) | |
(defn remove-fn-and-splice [fn-name form] | |
(if (and (list? form) | |
(= fn-name (first form))) | |
(apply concat (rest form)) | |
form)) | |
(defmacro print-form [form] | |
`(let [result# ~form | |
clean-form-fn# (partial remove-fn-and-splice 'me.andersfurseth.debug/print-form) | |
cleaned-form# (postwalk clean-form-fn# '~form) | |
cleaned-result# (postwalk clean-form-fn# result#)] | |
(println cleaned-form# "=>" cleaned-result#) | |
result#)) | |
(defn wrap-list-with-debug [form] | |
(if (list? form) ;; How can we detect quoted form? | |
`(print-form ~form) | |
form)) | |
(defmacro debug [form] | |
"Prints the form (and all nested forms) and their result to *stdout*." | |
(postwalk wrap-list-with-debug form)) | |
(comment | |
(debug (+ (+ 1 2 (+ 10 20 (+ 30 40))) (+ 3 4))) | |
(debug (let [a 1 | |
b 2] | |
(* (inc a) (inc b)))) | |
;; Fails, kinda. Result form this expansion still includes print-form | |
;; (1 [1 2 [3 4 [5 (eventum-api.debug/print-form (+ 6 7))]]]) | |
;; The printout is fine though, given cleaning of result, but we're actually | |
;; evaling the quoted form. | |
(debug (list 1 [1 2 [3 4 [5 '(+ 6 7) (+ 8 9)]]])) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment