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
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
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)
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
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)
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