Skip to content

Instantly share code, notes, and snippets.

@lbradstreet
Forked from noprompt/defntraced.clj
Created June 25, 2014 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbradstreet/9e7711dd6051f70c179c to your computer and use it in GitHub Desktop.
Save lbradstreet/9e7711dd6051f70c179c to your computer and use it in GitHub Desktop.
(defmacro defntraced
"Define a function with it's inputs and output logged to the console."
[sym & body]
(let [[_ _ [_ & specs]] (macroexpand `(defn ~sym ~@body))
new-specs (map
(fn [[args body]]
(let [prns (for [arg args]
`(js/console.log (str '~arg) "=" (pr-str ~arg)))]
(list
args
`(do
(js/console.groupCollapsed (str '~sym " " '~args))
~@prns
(let [res# ~body]
(js/console.log "=>" (pr-str res#))
(js/console.groupEnd)
res#)))))
specs)]
`(def ~sym (fn ~@new-specs))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment