doseq over atomic collection
Hi folks It may seem silly question but why when I doseq over a vector that is wrapped in an atom and change the vector using swap! while I am inside doseq, the doseq sets to the beginning of the vector intermittently: (def a-data (atom [15 9 8 1 4 11 7 12 13 14 5 3 16 2 10 6])) (defn switch-two-elements [the-vector] ... ) ;; swaping two elements in given vector (doseq [element @a-data] (println @a-data) (println element) (swap! a-data switch-two-elements)) [15 9 8 1 4 11 7 12 13 14 5 3 16 2 10 6] 15 [1 9 8 15 4 11 7 12 13 14 5 3 16 2 10 6] 9 [1 4 8 15 9 11 7 12 13 14 5 3 16 2 10 6] 8 [1 4 5 15 9 11 7 12 13 14 8 3 16 2 10 6] 1 == I expect this to be 15 I suspect the reason would be the same as the one behind not changing a collection in java while iterating over it. I appreciate any insight on this. Thanks a lot Best regards -- Software Architect Computer Scientist -- 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/d/optout.
Re: doseq over atomic collection
Dereferencing an atom will give you the value of the atom at the time you dereferenced it. So at the start of the doseq loop, you deref the atom and get back an immutable vector of values. It's the same as writing: (let [data @a-data] (doseq [element data] ...)) You dereference first, and then you iterate over that fixed sequence. The value of a-data might change, but the dereferenced data does not. - James On 5 April 2015 at 18:00, Shahrdad Shadab shahrd...@gmail.com wrote: Hi folks It may seem silly question but why when I doseq over a vector that is wrapped in an atom and change the vector using swap! while I am inside doseq, the doseq sets to the beginning of the vector intermittently: (def a-data (atom [15 9 8 1 4 11 7 12 13 14 5 3 16 2 10 6])) (defn switch-two-elements [the-vector] ... ) ;; swaping two elements in given vector (doseq [element @a-data] (println @a-data) (println element) (swap! a-data switch-two-elements)) [15 9 8 1 4 11 7 12 13 14 5 3 16 2 10 6] 15 [1 9 8 15 4 11 7 12 13 14 5 3 16 2 10 6] 9 [1 4 8 15 9 11 7 12 13 14 5 3 16 2 10 6] 8 [1 4 5 15 9 11 7 12 13 14 8 3 16 2 10 6] 1 == I expect this to be 15 I suspect the reason would be the same as the one behind not changing a collection in java while iterating over it. I appreciate any insight on this. Thanks a lot Best regards -- Software Architect Computer Scientist -- 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/d/optout. -- 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/d/optout.
Re: doseq over atomic collection
Thanks a lot James. It seems I completely missed the order of let and doseq. On Sun, Apr 5, 2015 at 1:12 PM, James Reeves ja...@booleanknot.com wrote: Dereferencing an atom will give you the value of the atom at the time you dereferenced it. So at the start of the doseq loop, you deref the atom and get back an immutable vector of values. It's the same as writing: (let [data @a-data] (doseq [element data] ...)) You dereference first, and then you iterate over that fixed sequence. The value of a-data might change, but the dereferenced data does not. - James On 5 April 2015 at 18:00, Shahrdad Shadab shahrd...@gmail.com wrote: Hi folks It may seem silly question but why when I doseq over a vector that is wrapped in an atom and change the vector using swap! while I am inside doseq, the doseq sets to the beginning of the vector intermittently: (def a-data (atom [15 9 8 1 4 11 7 12 13 14 5 3 16 2 10 6])) (defn switch-two-elements [the-vector] ... ) ;; swaping two elements in given vector (doseq [element @a-data] (println @a-data) (println element) (swap! a-data switch-two-elements)) [15 9 8 1 4 11 7 12 13 14 5 3 16 2 10 6] 15 [1 9 8 15 4 11 7 12 13 14 5 3 16 2 10 6] 9 [1 4 8 15 9 11 7 12 13 14 5 3 16 2 10 6] 8 [1 4 5 15 9 11 7 12 13 14 8 3 16 2 10 6] 1 == I expect this to be 15 I suspect the reason would be the same as the one behind not changing a collection in java while iterating over it. I appreciate any insight on this. Thanks a lot Best regards -- Software Architect Computer Scientist -- 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/d/optout. -- 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/d/optout. -- Software Architect Computer Scientist -- 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/d/optout.