This code probably will not make a whole lotta sense since I reduced
it down to show only the problem at hand, but I'm hoping someone can
explain why this doesn't work the way I expected it would:
=> (def data (atom {:k1 "v1" :k2 "v2" :k3 "v3"}))
#'user/data
=> (def flag (atom nil))
#'user/flag
=> (defn oops! []
(let [x1 (atom (hash-map))
v1 (filter
#(let [xv1 (@data %)]
(if (= xv1 "v1")
(swap! x1 assoc :k1 "other")))
(keys @data))
rxv (reset! flag @x1)]
(println v1)))
=> (oops!)
(:k1)
nil
=> @flag
{}
I had expected this flag would now hold the value from x1, but instead
it's empty.
Now if the only change I make is not to deref the x1 atom when
resetting:
=> (def flag (atom nil))
#'user/flag
=> (defn oops! []
(let [x1 (atom (hash-map))
v1 (filter
#(let [xv1 (@data %)]
(if (= xv1 "v1")
(swap! x1 assoc :k1 "other")))
(keys @data))
rxv (reset! flag x1)]
(println v1)))
=> (oops!)
(:k1)
nil
=> @flag
#<Atom@7dad453f: {:k1 "other"}>
The atom value is available.
So why didn't the first version with deref work?
Please & Thanks,
Tim
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en