Transducers for dummies
To be very honest, the first time transducers were presented, like a few other things by Rich Hickey, creator of Clojure, it felt like it was ahead of its time. Just even understanding the concepts was quite a task, never mind using them.
In this recipe, we will slowly look at how to use a transduce call, from the very first baby steps to some more pragmatic examples.
We will also go through the different terms used when dealing with transducers. Finally, we will also see how to apply parallelism to transducers using core.async
.
So relax, grab a bit of coffee, and let's get ready.
Getting ready
If you remember the into
function, you will remember that it takes all the elements from one collection and puts them in a different collection. You will mostly want to do so because the collection types are different or you want to get a sorted collection.
So, to turn a range-generated sequence into a vector, you would use into
:
(into [] (range 10)) ; [0 1 2 3 4...