Hey Ken, On 26 Jul 2011, at 09:45, Ken Wesson wrote: > > This seems to have been left out: > > (defn swap-and-also-return-old! [^clojure.lang.Atom a f] > (loop [] > (let [v @a > nv (f v)] > (if (compare-and-set! a v nv) > [nv v] > (recur))))) > > :)
Thanks for this :-) 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. 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... Sam --- http://sam.aaron.name -- 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