Closed hypirion closed 7 years ago
Yep, that would be a good idea.
Would it make sense for the functions that work on maps, like map-keys
and filter-vals
, to have transducer versions?
I don't think so, because transducers tend to work with some kind of ordered stream of data, and maps don't really fit into that category well. However, I might be wrong.
Transducer versions have been written for the named functions.
I think it would be useful to have transducer versions of the functions acting on maps, especially for multiple chained transformations on a single collection.
Essentially you could use (into {} xform coll)
and the reducing functions would act on a sequence of map-entry pairs - this would avoid having to call reduce-map
multiple times and create intermediate collections.
Here's an example, from a quick benchmark the transducer version performs 2x as fast:
;; Transducer
(into {}
(comp
(map name)
(index-by first)
(map-keys int)
(remove-vals #(even? (count %))))
[:abc :defg :hijkl :mnopq :rstu :vwx :yz])
;; => {97 "abc", 104 "hijkl", 109 "mnopq", 118 "vwx"}
;; Equivalent threading macro (creates intermediate colls)
(->> [:abc :defg :hijkl :mnopq :rstu :vwx :yz]
(map name)
(index-by first)
(map-keys int)
(remove-vals #(even? (count %))))
;; => {97 "abc", 104 "hijkl", 109 "mnopq", 118 "vwx"}
If this looks like a useful addition, I'll go ahead and open a PR :)
Yes, I think that seems useful.
Would it be interesting to provide transducer variants for the medley functions that runs over collections?
distinct-by
,take-upto
,drop-upto
,indexed
and probably some others do have transducer variants which could be useful.