Skip to content

Instantly share code, notes, and snippets.

@zwang
Created September 27, 2014 06:19
Show Gist options
  • Save zwang/5eb63c536bdc1fc70e86 to your computer and use it in GitHub Desktop.
Save zwang/5eb63c536bdc1fc70e86 to your computer and use it in GitHub Desktop.
clojure marco to enable code tracing
; From Clojure: the JFDI language by Michael O. Church
; https://docs.google.com/presentation/d/16ccLdZ6Epp6Nu-lMansEErwAjbN4sau4LztaARmdl_k/edit#slide=id.g1793dde48_1508
; Motivation: runtime tracing of functions is a powerful debug technique.
; It’d require a framework to do this in Java, but in Clojure, a macro suffices!
(defmacro tracing [code]
`(do
(println "The code was: " '~code)
(let [ret# ~code]
(println "Returning " ret#)
ret#)))
;; 2: do sequences multiple forms, returns last value (others
;; used for side effects!)
;; 3: '~code: unquote to insert the code, quote to block
;; run-time evaluation.
jfdi> (tracing (+ 5 6))
The code was: (+ 5 6)
Returning 11
11
jfdi> (+ 5 (tracing (* 3 6)))
The code was: (* 3 6)
Returning 18
23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment