Hi Eugen!

On Sun, Mar 14, 2010 at 6:51 AM, Eugen Dück <eu...@dueck.org> wrote:

> your fifo-strategy (the one that uses "identity" as the hit method)
> does not work:
>
> user=> (def g (memoize7 identity (fifo-strategy 3)))
> #'user/g
> user=> (g 1)
> 1
> user=> (g 1)
> java.lang.IllegalArgumentException: Wrong number of args passed to:
> core$identity (NO_SOURCE_FILE:0)
>
> You have to use something like #(first %&) instead of identity.
>

Yeah, you're right: I only tested with lru-strategy and forgot to replace
identity by (fn [mem args] mem) (like for the default strategy)


(defn memoize7-variant
>  ([f] (memoize7-variant f [{} identity (fn [mem args] mem) assoc]))
>  ([f [init cached hit assoc]]
>  (let [mem (atom init)]
>    (fn [& args]
>      (deref ((cached
>               (swap! mem (fn [mem]
>                            (if (contains? (cached mem) args)
>                             (hit mem args)
>                             (assoc mem args (delay (apply f args)))))))
>              args))))))
>
> It's more compact, as we do the contains? (or in your original
> version: "find") check only once inside the swap! function. This has
> the (admittedly not really huge) added benefit that we don't create
> multiple delays in the case of "bad luck", as mentioned in my earlier
> post.
>
> Any downsides to this?
>

Well the fn passed to swap! can be retried so in case of "bad luck" you'll
still create several delays.
However your version is interesting in that, while evolving one memoize into
another, I didn't realize that the first read wasn't needed anymore (now
that we have delays).

I updated my gist http://gist.github.com/330644

Thanks,

Christophe

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