It's worth knowing that the "moment" is implemented via a compare-and-set,
and if the value has been changed (by another thread), the fn you passed to
swap! will be called again with the atom's new value.
On Jun 28, 2013 11:57 PM, "Greg" <g...@kinostudios.com> wrote:

> OK, I've found something that shows how these two work when used in the
> implementation of a generator:
>
> https://gist.github.com/daveray/1263503
>
> I think I understand what the problem is now:
>
> (reset! id (inc @id))
>
> There's a "time gap" in between the dereference of 'id' and its assignment
> back to 'id' (after being incremented).
>
> With swap!, no such problem exists, because it's an atomic operation where
> there is no such "time gap" between dereferencing, applying a function, and
> setting the new value. That all takes place in on magical atomic moment.
>
> If I've got this wrong, please let me know!
>
> Cheers,
> Greg
>
> On Jun 28, 2013, at 11:19 PM, Greg <g...@kinostudios.com> wrote:
>
> Can anyone explain the relationship between swap! and reset! ?
>
> Why is using swap! in this example "safe" and using reset! not?
>
> I've tried searching google for comparisons of the two but can't find
> anything, and the documentation doesn't help much.
>
> Thanks,
> Greg
>
> On Jan 21, 2013, at 6:22 PM, Stephen Compall <stephen.comp...@gmail.com>
> wrote:
>
> On Jan 21, 2013 3:28 PM, "Jim - FooBar();" <jimpil1...@gmail.com> wrote:
> > ...or you can go all the way, skipping reset! completely:
> >
> > (swap! game-objects (fn [objects] (reduce-kv #(assoc % %2 (update-object
> %3)) {} objects) ))
>
> Which also has the benefit of being safe, unlike any reset!-based update.
>
> --
> Stephen Compall
> If anyone in the MSA is online, you should watch this flythrough.
>
> --
> 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 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.
>
>
>
>
>  --
> --
> 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.
>
>
>

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