Created
April 8, 2010 14:10
-
-
Save pervognsen/360102 to your computer and use it in GitHub Desktop.
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
(use '(clojure.contrib (str-utils :only (str-join)))) | |
(def print-trace println) ;; rebind locally as appropriate | |
(defmacro trace [form] | |
`(let [value# ~form] | |
(print-trace (format "%s => %s" '~form value#)) | |
value#)) | |
(defn trace-seq* [name value] | |
(lazy-seq | |
(if (seq value) | |
(do (print-trace (format "%s => (cons %s ...)" name (first value))) | |
(cons (first value) (trace-seq* name (rest value)))) | |
(do (print-trace (format "%s => nil" name)) | |
nil)))) | |
(defmacro trace-seq [form] | |
`(trace-seq* '~form ~form)) | |
(defn trace-fn* [name f] | |
(fn [& args] | |
(print-trace (format "(%s %s)..." name (str-join " " args))) | |
(let [return-value (apply f args)] | |
(print-trace (format "(%s %s) => %s" name (str-join " " args) return-value)) | |
return-value))) | |
(defmacro trace-fn [form] | |
`(trace-fn* '~form ~form)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment