I think I got it :) The two (swap! a inc) forms are added to the map at read-time - which is before they are evaluated. However, since we are associating the (swap! a inc) key with a value twice, only the last one counts. So the atom is inc'ed to 1 once (because keys can only be in the map once), and assoc'ed with the value 2 (because that's the second assoc we're doing to the (swap! a inc) form).
The PersistantArrayMap, on the other hand, is more gullible and trusting towards the parsed array of forms that the reader presents it, and therefor accepts the (swap! a inc) key twice, which in turn causes it to evaluate twice; into two different keys with two different associations. On Tue, Feb 17, 2009 at 8:28 PM, Christian Vest Hansen <karmazi...@gmail.com> wrote: > That's odd. > > Might you have uncovered a bug regarding: > > user=> (class {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9}) > clojure.lang.PersistentHashMap > user=> (class {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}) > clojure.lang.PersistentArrayMap > > While with the atom code we have: > > user=> (class (let [a (atom 0)] {(swap! a inc) 1 (swap! a inc) 2 3 3 4 > 4 5 5 6 6 7 7 8 8 9 9})) > clojure.lang.PersistentArrayMap > user=> (class (let [a (atom 0)] {(swap! a inc) 1 (swap! a inc) 2 3 3 4 > 4 5 5 6 6 7 7 8 8})) > clojure.lang.PersistentArrayMap > > > > On Tue, Feb 17, 2009 at 8:03 PM, Christophe Grand <christo...@cgrand.net> > wrote: >> >> While kicking the tires, I encountered this "behavior": >> >> user=> (let [a (atom 0)] {(swap! a inc) 1 (swap! a inc) 2 3 3 4 4 5 5 6 >> 6 7 7 8 8 9 9}) >> {3 3, 4 4, 5 5, 6 6, 7 7, 8 8, 9 9, 1 2} >> user=> (let [a (atom 0)] {(swap! a inc) 1 (swap! a inc) 2 3 3 4 4 5 5 6 >> 6 7 7 8 8}) >> {1 1, 2 2, 3 3, 4 4, 5 5, 6 6, 7 7, 8 8} >> >> Granted, it's not casual Clojure code but it's surprising. >> >> Christophe >> >> -- >> Professional: http://cgrand.net/ (fr) >> On Clojure: http://clj-me.blogspot.com/ (en) >> >> >> >> >> >> > > > > -- > Venlig hilsen / Kind regards, > Christian Vest Hansen. > -- Venlig hilsen / Kind regards, Christian Vest Hansen. --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---