In general, do not rely on the order of maps that are not explicitly sorted, and you will have fewer bugs in your code.
In this particular case, the order that seq returns the pairs of the hash map is in an order dependent upon the hash values of the keys. For integers in the range [0, Integer/MAX_VALUE], (hash x) is equal to x, and I believe that PersistentHashMap will often cause seq to traverse its elements in order of hash value, or something similar to it. Here is a modified example where the keys do not come back in sorted order: user=> (keys (frequencies [10 4294967295 9 4294967296 8 4294967297 7 4294967298 6 4294967299 5 4294967300 4 4294967301])) (4294967297 4294967296 4294967299 4294967298 4 4294967301 5 4294967300 6 7 8 9 10 4294967295) Andy On Mon, Sep 30, 2013 at 3:24 AM, Gavin Sinclair <gsincl...@gmail.com> wrote: > I have a value 'y' in my REPL that is a sequence containing two sequences > of numbers: > > (def y '( > (15 18 17 17 16 19 16 10 21 16 21 23 19 16 18 15 21 14 21 > 12 26 17 13 22 26 16 14 17 11 15 16 18 13 10 18 18 19 13 > 14 17 19 18 15 20 20 17 20 19 12 20 14 21 13 17 15 22 17 > 21 13 15 16 17 14 21 14 15 17 16 25 22 13 19 23 20 16 16 > 17 13 15 19 20 14 15 17 13 17 11 17 16 20 16 20 15 14 13 > 18 18 17 15 16 15 18 28 12 20 16 17 22 20 23 15 16 21 24 > 18 18 18 18 14 18 21 15 12 19 19 16 12 15 21 19 18 17 16 > 17 16 18 18 12 18 18 17 20 21 15 20 19 19 15 21 17 13 18 > 11 19 18 21 17 23 15 16 17 17 15 17 17 15 15 14 19 18 18 > 14 17 14 18 25 24 11 20 15 13 20 18 16 20 20 16 16 18 15 > 18 22 18) > > (24 18 20 17 19 19 20) > )) > When I ask for the frequencies of the two sequences, one is a sorted map > and the other is unsorted. > > => (keys (frequencies (first y))) > (10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28) > => (keys (frequencies (second y))) > (24 18 20 17 19) > > The classes of the maps don't indicate that the first map is indeed > sorted, but it cannot be a coincidence that they keys are returned in > order. (I tried it with some more data concatenated and it was still in > order.) > > The source for 'frequencies' (version 1.5.1) does not reveal to me why I > get sorted in one case and unsorted in the other. It must be to do with > the length of the sequence, but what is the full explanation? > > Thanks, > Gavin > > -- > -- > 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/groups/opt_out. > -- -- 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/groups/opt_out.