Hi Sam,

an another hardcore solution:

(defn update!
  [a f & args]
  (let [updated? (promise)
        watch (fn [k a o n] (remove-watch a k) (deliver updated? (not= o 
n)))]
    (add-watch a (Object.) watch)
    (apply swap! a f args)
    @updated?))

But...

Am Dienstag, 26. Juli 2011 10:56:25 UTC+2 schrieb Sam Aaron:

Actually I was just looking at compare-and-set! just now. This solution 
> seems nicer than Nick's 'place changed/old-val in atom' but still not 
> particularly clean as you have to enter a tight spin loop.


That's what swap! does anyway.
 

> With both solutions you have to write a certain amount of machinery - when 
> to my mind this is something a library/language should do for you as 
> boiler-plate machinery coding can be error-prone (and not everyone will code 
> it the same leading to slightly different versions with potentially 
> different efficiencies etc.) 
>
> I've also read other people's code that doesn't even use either of these 
> approaches, but implements something synonymous with my naive solution I 
> posted at the start of this thread. This leads me to believe that not 
> everyone is aware that you have to write a certain amount of machinery to 
> make comparing an atom's old and new vals thread safe.
>
> The additional 'return old and new vals' versions of swap! and reset! that 
> I propose place this machinery at the language level where I believe it 
> belongs. However, I'm also super interested to hear other people's thoughts 
> on this matter...
>
I'm still a bit unsure of why you need that. Maybe the fact, that you have 
to know the previous value (or at least whether the previous value was the 
same as the current one) is really part of your state? And should go into 
your domain value explicitly? The fact that you need special implementations 
for the update function for the different reference types, because the 
handling is intermingled with the actual implementation details of the 
reference types, somehow rings a bell with me. I can't explain this. Just 
some bad feeling in my stomach. ... Or maybe this is just reflux. YMMV.

Sincerely
Meikel

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