AOP tracing in clojure


/ Published in: Lisp
Save to your folder(s)

Shows how its possible to do AOP like programing in clojure.


Copy this code and paste it in your HTML
  1. (defn trace-wrap [v]
  2. (let [f (var-get v)
  3. fname (:name ^v)]
  4. (fn [& args]
  5. (println "calling" fname)
  6. (let [rtn (apply f args)]
  7. (println "done with" fname)
  8. rtn))))
  9.  
  10. (defn add [x y]
  11. (println "adding" x "and" y)
  12. (+ x y))
  13.  
  14. (prn (add 4 5))
  15.  
  16. (defmacro trace-fn [v & body]
  17. `(binding [~v (trace (var ~v))]
  18. ~@body))
  19.  
  20. (trace-fn add
  21. (prn (add 4 5)))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.