Hello,

Richelieu, a library for advising functions, is in something resembling 
announcement-worthy shape. It's available at the following URL:

http://github.com/thunknyc/richelieu

During my experience writing thunknyc/profile and the associated CIDER 
support, I realized that advising or decorating functions is something 
that's been getting reinvented over and over. I wanted to put an end to 
that. Richelieu supports advising functions as well as vars and namespaces. 
Multiple advise functions can be associated with a function, and advise 
functions have access to the underlying var or function this is being 
decorated. Below is an edited sample from the README that shows how to 
implement tracing advice using the library.

I hope this may be useful to one or more people out there. I plan on 
modifying thunknyc/profile to use Richelieu as part of a push to implement 
additional profiling modalities.

Regards,
Edwin

(require '[richelieu.core :refer [advice advise-ns
                                  *current-advised*
                                  defadvice]])

;;; Here are some simple functions.
(defn add [& xs] (apply + xs))
(defn mult [& xs] (apply * xs))
(defn sum-squares [& xs]
  (apply add (map #(mult % %) xs)))

;;; 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- ^:unadvisable 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-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 received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to