--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ycw4pZQBFfs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ycw4pZQBFfs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
(require '[richelieu.core :refer :all])
;;; Here are some simple functions. (defn add [& xs] (apply + xs)) (defn mult [& xs] (apply * xs)) (defn sum-squares [& xs] (apply add (map #(mult % %) xs)))
;;; `defadvice` is just a way to use `defn` with ':richelieu/no-advice` ;;; metadata to prevent crazy infinite advice loops. (defadvice plus1 "Adds one to each incoming argument, does nothing to the output." [f & xs] (apply f (map inc xs))) (defadvice times2 "Multiplies each incoming argument by two, does nothing to the output." [f & xs] (apply f (map (partial * 2) xs))) ;;; You can advise raw functions. (def add* (-> add (advise plus1) (advise times2))) (add* 1 1) ;;; But more often, you'll want to trace vars, which is what the rest ;;; of the example deals with.
;;; This tracing advice shows how to get the current advised object, ;;; which can either be a var or a function value, depending on the ;;; context in which the advice was added. (def ^:dynamic *trace-depth* 0
) (defn- ^:richelieu.core/no-advice trace-indent [] (apply str (repeat *trace-depth* \space))) (defadvice trace
"Writes passed arguments and passes them to underlying function. Writes resulting value before returning it as result." [f & args] (printf "%s> %s %s\n" (trace-indent) *current-advised* args) (let [res (binding [*trace-depth* (inc *trace-depth*)] (apply f args))] (printf "%s< %s %s\n"
(trace-indent) *current-advised* res) res)) (advise-var #'add trace) (unadvise-var #'add trace) ;;; This is safe because we used `defadvice` to prevent trace from ;;; advising itself--or other advice functions.
(advise-ns 'user trace) (sum-squares 1 2 3 4) ;;; The above invocation produces the following output: ;; > #'user/sum-squares (1 2 3 4) ;; > #'user/mult (1 1) ;; < #'user/mult 1 ;; > #'user/mult (2 2) ;; < #'user/mult 4 ;; > #'user/mult (3 3) ;; < #'user/mult 9 ;; > #'user/mult (4 4) ;; < #'user/mult 16 ;; > #'user/add (1 4 9 16) ;; < #'user/add 30 ;; < #'user/sum-squares 30
;;; You can also suppress the evalutation of advice with the ;;; `without-advice` macro. For example, the following will produce ;;; no tracing output, but will allow any other advice that (possibly ;;; someone else) attached to any function. (without-advice [trace] (sum-squares (1 2 3 4))) ;; ==> 30, no tracing ;;; Finally, it will often be a good idea to refer to advice functions ;;; via var quoting instead of by a simple reference. This will allow ;;; you to redefine them during development and still add or remove old ;;; versions of attached advice functions, because they will be ;;; associated with the var, not the particular function value that the ;;; var pointed to at the time. (advise-ns 'user trace) ;; This works great until you re-eval ;; your `(defadvice trace ...)` form. (advise-ns 'user #'trace) ;; Infinitesimally slower but highly ;; recommended.
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ycw4pZQBFfs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+unsubscribe@googlegroups.com.