Details
-
Type:
Enhancement
-
Status:
Open
-
Priority:
Minor
-
Resolution: Unresolved
-
Affects Version/s: Release 1.5
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Patch:Code
Description
Currently, although rfn makes an effort to support reduce-kv, it is wasted effort since the return values of reducer and folder don't satisfy IKVReduce.
I have a working patch for this, it's quite small. I have printed a CA but not sent it, so I won't reveal the patch yet...
This also applies to ClojureScript.
Hmm.. it's not quite as simple as I thought. My first patch simply had reducer/folder implement IKVReduce with `(clojure.core.protocols/kv-reduce coll (xf f1) init)`, but for map and mapcat, this results in strange behavior (reduce-kv reducefs being called with only 2 args).
Patch 0001 includes modifications to map and mapcat so that the reduce-kv reducef will always be called with 3 args. The previous implementation of mapcat also had the converse problem: during non-kv reduce, if the mapcat fn returned a map, the reducef would be called with 3 args since maps reduce with reduce-kv.
The patch gives behavior like:
user> (->> {1 {2 3 4 5} 6 {7 8 9 10}} (r/mapcat #(r/map + %2)) (reduce-kv #(conj %1 [%2 %3]) [])) [[2 5] [4 9] [7 15] [9 19]] user> (->> [{2 3 4 5} {7 8 9 10}] (r/mapcat identity) (r/reduce conj [])) [[2 3] [4 5] [7 8] [9 10]]user> (->> {1 {2 3 4 5} 6 {7 8 9 10}} (r/mapcat #(r/map + %2)) (reduce-kv #(conj %1 [%2 %3]) [])) [[2 5] [4 9] [7 15] [9 19]] user> (->> [{2 3 4 5} {7 8 9 10}] (r/mapcat identity) (r/reduce conj [])) [[2 3] [4 5] [7 8] [9 10]]