I'm trying to figure out the best way to use clojure.core/memoize
while retaining the original function's docstrings, tags, etc. for the
memoized function. The best way I've seen to do this is James Reeves'
decorate-with function from Compojure, which applies a "decorator" --
Python's name for a higher-order function, like memoize, that takes a
function f & returns f' that's a version of f modified in some useful
way -- to already-defined functions, like

 (defn costly-fn [x]
    (costly-computation x))

 (decorate-with memoize costly-fn)

The most useful thing about decorate-with is its side-effecting code
that redefines the root binding of the costly-fn var while keeping its
original metadata. This is done by the redef function, which is (with
a few minor differences from the Compojure version):

 (defmacro redef
   "Redefine an existing value, keeping the metadata intact."
   [name value]
   `(let [m# (meta (var ~name))
          v# (def ~name ~value)]
      (alter-meta! v# merge m#)
      v#))

Question: is there a better way of using decorators than this? If not,
is there any interest in including redef & decorate-with (see below)
in contrib, either, perhaps, in c.c.def or c.c.macros?

Thanks,
Perry

(defmacro decorate-with
  "Wrap functions in a decorator."
  [decorator & funcs]
  `(do ~@(for [f funcs]
          `(redef ~f (~decorator ~f)))))

Compojure's compojure.control library: http://bit.ly/17U7m

--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to