On Tuesday, October 25, 2011 12:00:04 PM UTC-4, Tim Robinson wrote:
>
> 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. 
>
> No, it's not empty - it holds the value that x1 had at the time you swapped 
it, which is an empty map.

Later, the x1 atom had its value changed, by then it was too late to affect 
flag. Specifically, x1 changed only when you printed v1, thus forcing 
realization of the lazy sequence that filter returned.

Make sense?

- Chris

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