Created
September 27, 2014 06:19
-
-
Save zwang/5eb63c536bdc1fc70e86 to your computer and use it in GitHub Desktop.
clojure marco to enable code tracing
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
; 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