On Fri, Apr 1, 2011 at 1:59 AM, Laurent PETIT <laurent.pe...@gmail.com> wrote:
> 2011/4/1 Ken Wesson <kwess...@gmail.com>
>>
>> On Thu, Mar 31, 2011 at 3:24 PM, Laurent PETIT <laurent.pe...@gmail.com>
>> wrote:
>> > 2011/3/31 Ken Wesson <kwess...@gmail.com>
>> >>
>> >> On Wed, Mar 30, 2011 at 12:02 PM, Laurent PETIT
>> >> <laurent.pe...@gmail.com>
>> >> wrote:
>> >> > Except in 1.3 it will be a little bit harder to do "throw-away"
>> >> > per-thread
>> >> > memoizes for vars you do no "own" if their author didn't make their
>> >> > holding
>> >> > var :dynamic ('cause then you will not be able to rebind them).
>> >> >
>> >> > This is close to being a problem for paredit.clj (not tested yet),
>> >> > but
>> >> > hopefully I'll discover that I'm using memoize this way with
>> >> > paredit's
>> >> > own
>> >> > functions only ...
>> >>
>> >> You'll still be able to use
>> >>
>> >> (ns foo.core
>> >>  (use (somens.baz :exclude quux)))
>> >>
>> >> (def quux (memoize somens.baz/quux))
>> >>
>> >> (code that
>> >>  (uses quux
>> >>    (goes here)))
>> >
>> > Indeed but that's not what I meant to express. I was talking about a
>> > more
>> > "volatile" memoization, one which does not stay around until the
>> > foo.core/quux root's value is explicitly replaced (and hopefully garbage
>> > collected).
>>
>> Well, there's always
>>
>> (ns foo,.core
>>  (use ...))
>>
>> (some code
>>  (let [quux (memoize quux)]
>>    (more code)))
>>
>> when you want the memoized version locally, and don't mind creating it
>> anew (with no memory) each time a particular code path is called.
>
> Well, of course, but then it's lexically scoped, and you'll have to pass it
> around to every function which may need it (or who calls a function which
> may need it).
> Not so with a carefully rebound var.

If you want old code X to use a memoized version of a function f
defined in old code Y, and X calls f via its var, then you have no
choice but to use binding or def to replace f with its memoized
version globally.

If it's only direct calls to f from your own code you want to memoize,
though, and you want to dynamically scope the memoization and gc it
later:

(ns foo.core
  (use (somens.baz :exclude quux)))

(def ^:dynamic quux somens.baz/quux)

(code that
  (uses quux
    (goes here)))

(defn somefn-that-wants-quux-memoized-during-its-execution []
  (binding [quux (memoize quux)]
    (code that
      (uses memoized quux
        (and calls functions
          (we want to use memoized quux
            (goes here)))))))

As long as the called functions look up quux via foo.core/quux and not
somens.baz/quux they'll get the memoized version if called during the
execution of the above function.

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