clojure.core/keys
Why does the keys function return unexpected order when there are 8 or more keys in a map? I believe the order issue also applies to seq. -- 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: clojure.core/keys
Except for sorted-map and a few other special kinds of maps with ordering guarantees, the normal kind of map in Clojure is a hash-map, which uses a hash function of the keys to create a tree data structure with the hash value as a search key. seq on hash-map's traverses these trees in a deterministic order, so the return order of keys from the clojure.core/keys or clojure.core/seq functions are in increasing order of hash value of the keys (or maybe it is decreasing -- either way, it is independent of the order that you added the keys to the map). For memory and run-time efficiency, maps that have 8 or fewer keys in them are implemented with a different concrete data structure called array-map's. Those do happen to give you back the elements in the order that the keys were first added, but as soon as you add a 9th key, the implementation switches to a hash-map, and the order is then unrelated to the order that keys were added. If you want a map that is guaranteed to return the keys in the order they were added, there is an implementation of this available called ordered-maps's here: https://github.com/amalloy/ordered Andy On Sun, Aug 5, 2018 at 12:12 AM Andres Pineda wrote: > Why does the keys function return unexpected order when there are 8 or > more keys in a map? > > I believe the order issue also applies to seq. > > -- > 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: clojure.core/keys
Thanks for clearing that up. -Andres On Sunday, August 5, 2018 at 12:17:47 AM UTC-7, Andy Fingerhut wrote: > > Except for sorted-map and a few other special kinds of maps with ordering > guarantees, the normal kind of map in Clojure is a hash-map, which uses a > hash function of the keys to create a tree data structure with the hash > value as a search key. seq on hash-map's traverses these trees in a > deterministic order, so the return order of keys from the clojure.core/keys > or clojure.core/seq functions are in increasing order of hash value of the > keys (or maybe it is decreasing -- either way, it is independent of the > order that you added the keys to the map). > > For memory and run-time efficiency, maps that have 8 or fewer keys in them > are implemented with a different concrete data structure called > array-map's. Those do happen to give you back the elements in the order > that the keys were first added, but as soon as you add a 9th key, the > implementation switches to a hash-map, and the order is then unrelated to > the order that keys were added. > > If you want a map that is guaranteed to return the keys in the order they > were added, there is an implementation of this available called > ordered-maps's here: https://github.com/amalloy/ordered > > Andy > > On Sun, Aug 5, 2018 at 12:12 AM Andres Pineda > wrote: > >> Why does the keys function return unexpected order when there are 8 or >> more keys in a map? >> >> I believe the order issue also applies to seq. >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@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+u...@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+u...@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.