On Sun, Oct 4, 2009 at 4:41 PM, samppi <rbysam...@gmail.com> wrote:
>
> I want to do this:
>
>  (defn a ...)
>  (cache a) ; or (cache #'a) or (cache 'a); it doesn't matter to me
>
> ...instead of this:
>
>  (def a (memoize (fn ...)))
>
> That way, it separates the concern of what a does from the
> optimization I'm doing on it. Now, I'm kind of stuck; how should I do
> it?
>
>  (defn cache
>    "Replaces the function that the given variable refers to
>    with a memoizing version of it."
>    [fn-var]
>    (??? fn-var (memoize @fn-var)))

Test function:

(defn foo [i] (println "cache-miss") i)

user=> (foo 5)
cache-miss
5

Utility:
(defn memoize-var [v] (alter-var-root v memoize))

Example usage:
(memoize-var #'foo)

user=> (foo 5)
cache-miss
5
user=> (foo 5)
5

--Chouser

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

Reply via email to