I'm afraid your understanding of atom swap! operations is not quite correct 
-> update function must (or should) be pure as well.
See the 
documentation<http://clojuredocs.org/clojure_core/clojure.core/swap!>for swap!, 
update function could be potentially called multiple times if 
there are more threads of execution
updating one atomic reference -> there is simple compare and 
set<http://clojuredocs.org/clojure_core/clojure.core/compare-and-set!>mechanism 
involved, which compares the old value of the atom
(input for the update function) and sets the atom to new value (return 
value from the update function) only value of the atom reference
was not changed in between, if yes the compare and set is retried until ti 
succeeds.

Dňa nedeľa, 16. februára 2014 23:35:24 UTC+1 t x napísal(-a):
>
> I believe that's the STM approach, which has the advanrtage of: 
>
>   * can synchronize across multiple pieces of data 
>
> but has the disadvantage of: 
>
>   * work must be "pure" since it can be retried 
>
>   * possibly less efficient due to possibility of retrying 
>
>
> In the example I posted above, I only need to: 
>
>   * modify a single atom 
>
> and the approach I presented above: 
>
>   * can be impure, since it is guaranteed to only run once 
>
>   * is guaranteed to succeed (without retrys) 
>
> On Sun, Feb 16, 2014 at 2:25 PM, Ramesh <ramesh1...@gmail.com<javascript:>> 
> wrote: 
> > You can use a ref instead of an atom. And use dosync with multiple 
> alter, so 
> > everything is safely inside a transaction. 
> > 
> > On Feb 16, 2014 2:04 PM, "t x" <txre...@gmail.com <javascript:>> wrote: 
> >> 
> >> Hi John, 
> >> 
> >>   Your solution is perfectly valid and optimal for the problem I 
> >> described above. 
> >> 
> >> 
> >>   Unfortunately, I forgot to mention an additional constraint: 
> sometimes I 
> >> do: 
> >> 
> >> (let [ foo (:some-selector @data-atom) ] 
> >>   (swap! data-atom update-in [:other-selector] ... )) 
> >> 
> >> which the -> doesn't quite work on, but my ugly hack above does 
> resolve. 
> >> 
> >> 
> >>   The problem being -- I want to update one part of the atom, but it 
> >> depends on another part of the atom. 
> >> 
> >>   I ended up with the following hack: 
> >> 
> >> (defn tswap! [atm func] 
> >>   (swap! atm 
> >>          (fn [old] 
> >>            (let [natm (atom old)] 
> >>              (func natm) 
> >>              @natm)))) 
> >> 
> >> 
> >> On Sat, Feb 15, 2014 at 4:09 PM, John D. Hume 
> >> <duelin....@gmail.com<javascript:>> 
>
> >> wrote: 
> >> > On Sat, Feb 15, 2014 at 6:04 PM, t x <txre...@gmail.com <javascript:>> 
> wrote: 
> >> >> 
> >> >> 
> >> >> (defn what-I-want [] 
> >> >>   (with-atom some-atom 
> >> >>     assoc-in ... 
> >> >>     assoc-in ... 
> >> >>     update-in ...)) 
> >> > 
> >> > 
> >> > I often do something like this and don't find it too ugly: 
> >> > (swap! my-atom #(-> % 
> >> >                   (assoc-in [:k] v) 
> >> >                   (update-in [:k2] inc) 
> >> >                   ,,,)) 
> >> > 
> >> > -- 
> >> > You received this message because you are subscribed to the Google 
> >> > Groups "Clojure" group. 
> >> > To post to this group, send email to 
> >> > clo...@googlegroups.com<javascript:> 
> >> > Note that posts from new members are moderated - please be patient 
> with 
> >> > your 
> >> > first post. 
> >> > To unsubscribe from this group, send email to 
> >> > clojure+u...@googlegroups.com <javascript:> 
> >> > 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+u...@googlegroups.com <javascript:>. 
> >> > For more options, visit https://groups.google.com/groups/opt_out. 
> >> 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> >> Groups "Clojure" group. 
> >> To post to this group, send email to clo...@googlegroups.com<javascript:> 
> >> Note that posts from new members are moderated - please be patient with 
> >> your first post. 
> >> To unsubscribe from this group, send email to 
> >> clojure+u...@googlegroups.com <javascript:> 
> >> 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+u...@googlegroups.com <javascript:>. 
> >> For more options, visit https://groups.google.com/groups/opt_out. 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> > Groups "Clojure" group. 
> > To post to this group, send email to clo...@googlegroups.com<javascript:> 
> > Note that posts from new members are moderated - please be patient with 
> your 
> > first post. 
> > To unsubscribe from this group, send email to 
> > clojure+u...@googlegroups.com <javascript:> 
> > 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+u...@googlegroups.com <javascript:>. 
> > For more options, visit https://groups.google.com/groups/opt_out. 
>

-- 
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/groups/opt_out.

Reply via email to