Welcome! Please see the About page for a little more info on how this works.

+3 votes
in data.int-map by

Why does reduce-kv on a priority-map call the reducing function with item as the key and priority as the value (source) instead of the map's key and value. Calling reduce will be passed the map's key and value. I'm assuming it has something to do with this particular data structure's origins, but I cannot find any documentation pointing me to in in the clojure.data.priority-map ns. Here is an example.

(def m
  (clojure.data.priority-map/priority-map-keyfn-by first compare
    :a [2 :apples] :b [1 :bananas]))

(reduce-kv
  (fn [acc k v]
    (assoc acc k v))
  {} m)
=> {:b 1, :a 2}

(reduce
  (fn [acc [k v]]
    (assoc acc k v))
  {} m)
=> {:b [1 :bananas], :a [2 :apples]}

Is this the expected behavior? If so, could we add some documentation that this is how reduce-kv on a PersistentPriorityMap should work?

1 Answer

0 votes
by

It should be a bug in there
https://github.com/clojure/data.priority-map/blob/master/src/main/clojure/clojure/data/priority_map.clj#L396

Maybe (reduce (fn [a v] (f a v k)) a [k v]) will be the right solution. Not shure

...