Skip to content

Instantly share code, notes, and snippets.

@ordnungswidrig ordnungswidrig/formlog.clj Secret
Created Jan 19, 2011

What would you like to do?
expression logging in clojure
(ns formlog)
(declare *log*)
(defn make-log [] (ref []))
(defmacro logged [& forms]
(let [form (gensym "form")]
~@(map (fn [form]
`(do (alter *log* conj '~form)
(defn playlog [log]
(for [form @log]
(eval form)))))
(defmacro with-log [log-ref & body]
`(binding [*log* ~log-ref]
(defn run-test-1 []
(let [counter (ref nil)
log (make-log)]
(with-log log
(logged (ref-set counter 0))
(logged (alter counter inc ))
(logged (alter counter inc))
(logged (alter counter #(* % %))
(alter counter * 2)))
(prn @counter)
(prn @log)
;; alter counter outside with-log...
(with-log log
(dosync (alter counter dec)))
(prn @counter)
;; ... is not registered in log
(prn @log)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.