weavejester / medley

A lightweight library of useful Clojure functions
Eclipse Public License 1.0
865 stars 66 forks source link

Transducer versions for functions over collections #12

Closed hypirion closed 7 years ago

hypirion commented 8 years ago

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.

weavejester commented 8 years ago

Yep, that would be a good idea.

madstap commented 7 years ago

Would it make sense for the functions that work on maps, like map-keys and filter-vals, to have transducer versions?

weavejester commented 7 years ago

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.

weavejester commented 7 years ago

Transducer versions have been written for the named functions.

yuhan0 commented 5 years ago

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

weavejester commented 5 years ago

Yes, I think that seems useful.