Re: determining whether state has changed in a thread safe manner

2011-07-26 Thread Meikel Brandmeyer
Hi, Am 25.07.2011 um 23:58 schrieb Sam Aaron: > Sorry, I should have been more specific. The callback-based watchers are > cool, but I don't believe they specifically address my problem (which I don't > believe I sufficiently explained from the outset). > > Hopefully this is a more succinct an

Aw: Re: determining whether state has changed in a thread safe manner

2011-07-26 Thread Meikel Brandmeyer
Hi again, Am Dienstag, 26. Juli 2011 11:35:08 UTC+2 schrieb Meikel Brandmeyer: > (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) > @

Re: determining whether state has changed in a thread safe manner

2011-07-26 Thread Meikel Brandmeyer
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 U

Re: determining whether state has changed in a thread safe manner

2011-07-26 Thread Ken Wesson
On Tue, Jul 26, 2011 at 4:56 AM, Sam Aaron wrote: > Hey Ken, > > Thanks for this :-) You're welcome. > 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 tig

Re: determining whether state has changed in a thread safe manner

2011-07-26 Thread Sam Aaron
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 t

Re: determining whether state has changed in a thread safe manner

2011-07-26 Thread Ken Wesson
On Tue, Jul 26, 2011 at 4:00 AM, Sam Aaron wrote: > However, it seems remarkably kludgy to encode the old value into the contract > of the atom and *all* update fns when really this could be achieved in a much > cleaner fashion with a version of swap! that returned a vec of new and old > vals:

Re: determining whether state has changed in a thread safe manner

2011-07-26 Thread Sam Aaron
Hi Nick, On 25 Jul 2011, at 23:55, cassiel wrote: > > Not very practical, but if you want a safe transaction-free operation > on an atom which returns whether it was changed, you can perhaps hack > it by embedding the change state into the atom itself: > > (def a (atom {:value 45 :changed? false

Re: determining whether state has changed in a thread safe manner

2011-07-25 Thread cassiel
Of course, once posted, I realised the conditional could be eliminated: (defn update-and-check-whether-modified? [update-fn] (:changed? (swap! a (fn [{v :value _ :changed?}] (let [new-v (update-fn v)] {:value new-v :changed? (not= v new-v)}) -- You receiv

Re: determining whether state has changed in a thread safe manner

2011-07-25 Thread cassiel
Hi Sam, A nice late night exercise... Not very practical, but if you want a safe transaction-free operation on an atom which returns whether it was changed, you can perhaps hack it by embedding the change state into the atom itself: (def a (atom {:value 45 :changed? false})) (defn update-and-ch

Re: determining whether state has changed in a thread safe manner

2011-07-25 Thread Sam Aaron
Hi Meikel, On 25 Jul 2011, at 22:46, Meikel Brandmeyer wrote: > Am 25.07.2011 um 23:12 schrieb Sam Aaron: > > Since this is callback based, you can't return a value. Do you want more > something like a polling solution? Then you'll have to roll your own with an > atom and a clojure.lang.Persis

Re: determining whether state has changed in a thread safe manner

2011-07-25 Thread Meikel Brandmeyer
Hi, Am 25.07.2011 um 23:12 schrieb Sam Aaron: > So what if I would like #'update to return a boolean representing whether the > state has changed rather than call #'changed-fn internally? (which is > actually what i'm trying to do, I just refactored the example to be a lot > simpler and hopefu

Re: determining whether state has changed in a thread safe manner

2011-07-25 Thread Sam Aaron
Hi Meikel, On 25 Jul 2011, at 21:51, Meikel Brandmeyer wrote: > > you want a watch. > > (def a (atom 0)) > (add-watch a ::your-id (fn [_your-id _a old-val new-val] (when (not= old-val > new-val) (println "New value:" new-val > (swap! a inc) > (reset! a 1) > (swap! a inc) > That's cool to

Re: determining whether state has changed in a thread safe manner

2011-07-25 Thread Meikel Brandmeyer
Hi, you want a watch. (def a (atom 0)) (add-watch a ::your-id (fn [_your-id _a old-val new-val] (when (not= old-val new-val) (println "New value:" new-val (swap! a inc) (reset! a 1) (swap! a inc) Sincerely Meikel -- You received this message because you are subscribed to the Google Groups

Re: determining whether state has changed in a thread safe manner

2011-07-25 Thread Sam Aaron
On 25 Jul 2011, at 21:45, Sam Aaron wrote: > > (defn update > [] > (let [changed? (dosync > (let [old-a @a >new-a (ref-set a (new-val))] >(= old-a new-a)))] >(when changed? (changed-fn Clearly I meant (not (= old-a new-a)) :-

determining whether state has changed in a thread safe manner

2011-07-25 Thread Sam Aaron
Hi there, I have some state which I'd like to set to some default value, A. I'd then like to update A to a new value A' and then, if (not (= A A')) I'd like to fire off a function - say print to stdout that A has changed. If (= A A') I'd like nothing to happen at all. Additionally, I'd like to