The java core library also provides LinkedHashMap which preserves insertion order, although this is a mutable bash-in-place data structure rather than an immutable persistent data structure. On Jun 6, 2013 4:06 PM, "Andy Fingerhut" <andy.finger...@gmail.com> wrote:
> A few people, I believe primarily Alan Malloy and Anthony Grimes, have > created a Clojure library for what they call ordered sets and maps that do > exactly this. They are implemented not as you tried to do, but by > remembering a number for each element (for ordered sets) or key (for > ordered maps) that is the relative order that it was added in. > > https://github.com/flatland/ordered > > Andy > > > On Thu, Jun 6, 2013 at 7:56 AM, dennis zhuang <killme2...@gmail.com>wrote: > >> Thanks,you are right.I want to creat a map which keeps elements in >> insertion order, but clojure doesn‘t have. >> 在 2013-6-6 下午10:02,"Andy Fingerhut" <andy.finger...@gmail.com>写道: >> >> Your comparator #(if (= %1 %2) 0 1) may happen to give the correct >>> answers for your example sorted-maps, but it is also a bad comparator that >>> will fail for larger examples: >>> >>> user=> (:a (sorted-map-by #(if (= %1 %2) 0 1) :z -26 :b 1 :a 2 :c 3 :m >>> 13 :h 8)) >>> nil >>> user=> (:z (sorted-map-by #(if (= %1 %2) 0 1) :z -26 :b 1 :a 2 :c 3 :m >>> 13 :h 8)) >>> nil >>> >>> That is because if two items are not =, by returning 1 you are telling >>> the caller "the first argument should come after the second argument". >>> Thus if at some time the comparator is called as (cmp :a :z), and later it >>> is called as (cmp :z :a), it returns the inconsistent results that :a >>> should come after :z, and later that :z should come after :a. No sorted >>> tree can hope to return correct results given such an inconsistent >>> comparator. >>> >>> More examples and discussion at the link below, if you are interested: >>> >>> >>> https://github.com/jafingerhut/thalia/blob/master/doc/other-topics/comparators.md >>> >>> Andy >>> >>> >>> On Thu, Jun 6, 2013 at 4:19 AM, dennis zhuang <killme2...@gmail.com>wrote: >>> >>>> Sorry, it's my mistake. >>>> Because treep map use the comparator to compare keys, and if the >>>> comparator returns 1 constantly,it can not find the item that equals the >>>> key. >>>> >>>> So i can modified the example,and it works: >>>> >>>> user=> (sorted-map-by #(if (= %1 %2) 0 1) :b 1 :a 2) >>>> {:b 1, :a 2} >>>> user=> (:a (sorted-map-by #(if (= %1 %2) 0 1) :b 1 :a 2)) >>>> 2 >>>> user=> (:b (sorted-map-by #(if (= %1 %2) 0 1) :b 1 :a 2)) >>>> 1 >>>> >>>> >>>> 2013/6/6 dennis zhuang <killme2...@gmail.com> >>>> >>>>> user=> (sorted-map-by (constantly 1) :b 1 :a 2) >>>>> {:b 1, :a 2} >>>>> user=> (:a (sorted-map-by (constantly 1) :b 1 :a 2)) >>>>> nil >>>>> user=> (keys (sorted-map-by (constantly 1) :b 1 :a 2)) >>>>> (:b :a) >>>>> user=> (count (sorted-map-by (constantly 1) :b 1 :a 2)) >>>>> 2 >>>>> user=> (:a (sorted-map-by (constantly 1) :b 1 :a 2)) >>>>> nil >>>>> >>>>> It looks so strange.The result map has keys :a and :b,but i can't get >>>>> their values. >>>>> Why? I try to hack the code,but i can't find the reason. >>>>> >>>>> >>>>> -- >>>>> 庄晓丹 >>>>> Email: killme2...@gmail.com xzhu...@avos.com >>>>> Site: http://fnil.net >>>>> Twitter: @killme2008 >>>>> >>>>> >>>>> >>>> >>>> >>>> -- >>>> 庄晓丹 >>>> Email: killme2...@gmail.com xzhu...@avos.com >>>> Site: http://fnil.net >>>> Twitter: @killme2008 >>>> >>>> >>>> -- >>>> -- >>>> 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. >>> >>> >>> >> -- >> -- >> 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. > > > -- -- 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.