On 11 July 2010 12:38, Jeffrey Schwab <j...@schwabcenter.com> wrote:
> Is it safe to alter an atom while iterating that atom's value?  It seems to
> work as I would expect, but am I just getting lucky (and are there
> concurrency issues)?
>
> user=> (let [a (atom #{1 2 3 4 5})]
>         (doseq [x @a] (prn @a) (swap! a disj x)))

Whenever you deref an atom (using @), you're getting a static value of
the atom at that instance in time.

Your example code is logically equivalent to:

(let [a (atom #{1 2 3 4 5})]
  (doseq [x #{1 2 3 4 5}]
    (prn @a)
    (swap! a disj x)))

You're not iterating over the atom per se; you're just iterating over
the value of the atom at that point in time.

So yes, it's safe. This is one of the benefits of Clojure concurrency
constructs - you're not changing the value inside, you're just
pointing to a different value.

- James

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