Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created April 8, 2010 14:10
Show Gist options
  • Save pervognsen/360102 to your computer and use it in GitHub Desktop.
Save pervognsen/360102 to your computer and use it in GitHub Desktop.
(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