Clojure Priority Card

I have the following:

(let [t (priority-map-by (comparator (fn [[f1 _] [f2 _]] (< f1 f2))) :b [8 [2 1]])] (assoc t :c [8 [2 3]])) 

for some reason, the related element: c changes after adding to the map, what I get is

 {:c [8 [2 1]], :b [8 [2 1]]} 

what i expect

 {:c [8 [2 3]], :b [8 [2 1]]} 

I am missing something obvious, why does the meaning of the elements change? or is it a mistake?

+4
source share
2 answers

I got what I wanted without using a comparator and returning -1 0 1 depending on cond,

 (let [t (priority-map-by (fn [xy] (if (= xy) 0 (let [[f1 _] x [f2 _] y] (if (< f1 f2) -1 1)))) :b [8 [2 1]])] (assoc t :c [8 [2 3]])) 

which gives me what I want

 {:b [8 [2 1]], :c [8 [2 3]]} 
+1
source

Considering the implementation for priority-map-by , it seems to be based on sorted-map-by , which contains the keys that compare them, the same thing. For instance:

 (sorted-map-by (fn [[a] [b]] (< ab)) [1 2] :foo [1 3] :bar) => {[1 2] :bar} 

This is also true for sorted-set-by :

 (sorted-set-by (fn [[a] [b]] (< ab)) [1 2] [1 3] [3 4]) => #{[1 2] [3 4]} 

What is the intentional behavior for priority-map-by , you should ask the author. I think this is reasonable.

0
source

All Articles