>
>
> Most of the cache implementations in core.cache have no side-effects. They
> simply return a new cache rather than overwriting the old one. The memoize
> library places the cache in an atom, so it's guaranteed to change
> atomically.
>


I tried to read the cache code (btw an excellent exercise) , and I think I
understand how persistent data structure/atom is employed here in case we
deal with side effects free functions.


> You could write this as a function. There's nothing in there that requires
> a macro.
>
> (defn when-map-future-swap! [a k f]
>   (locking a
>     (when-not (contains? @a k)
>       (swap! a assoc k nil)
>       (future (swap! a assoc k (f k))))))
>

I realized that later too ...

But I'd personally just use a delay rather than "locking" for this purpose.
>

It is not that I like locking at all. However I still fail to see, how in a
multithreaded context memoize/cache prevents executing a given function
more than once (which I want to avoid at any cost here) since cache lookup
and swap! does not seem to be atomic :
https://github.com/clojure/core.cache/blob/master/src/main/clojure/clojure/core/cache.clj#L52
.

Best regards,
Andy

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to