On Mon, Aug 25, 2008 at 11:57 AM, Rich Hickey <[EMAIL PROTECTED]> wrote:
>
> (def hits (ref 0))
> (def cache (ref {:n nil :factors nil}))
> (def cache-hits (ref 0))
>
> (defn cached-factor [n]
> (dosync (commute hits inc))
> (let [cached @cache]
> (if (= n (cached :n))
> (dosync (commute cache-hits inc)
> (cached :factors))
> (let [factors (factor n)]
> (dosync
> (commute cache (fn [_] {:n n :factors factors})))
> factors))))
>
> The ref-sets were needed in your example, but with a composite cache,
> you can use the commute trick above (commute with a fn that ignores
> its arg) to get last-one-in-wins, since this is just a last-value-only
> cache.
>
Is there a stylistic or correctness advantage to using commute over ref-set
on the composite cache? As in:
(dosync (ref-set cache {:n n :factors factors}))
--Shawn
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---