(Question credit: Fernando Abrao.)
I hear about the performance benefits of transducers in Clojure, but I'm not sure how to use them.
Say I have a qos/device-qos-range function that returns sequence of maps, some of which contain a decimal :samplevalue, like so:
[
{ :samplevalue 1.3, ... },
{ :othervalue -27.7, ... },
{ :samplevalue 7.5, ... },
{ :samplevalue 1.9, ... },
]
I'd like to see how many :samplevalues fall into each integer bin, like so:
(frequencies
(reduce #(if (not (nil? (:samplevalue %2)))
(conj %1 (.intValue (:samplevalue %2))))
[]
(qos/device-qos-range origem device qos alvo inicio fim)))
;; => {1 2, 7 1}
How can I turn this into a fast version with transducers that eliminates intermediate data structures (such as the one returned by reduce)? Bonus points for code that can take advantage of multiple cores to do parallel processing.