Transducer Explained
A minuscule guide about transducers
What follow is a very small walkthrought of transducers for my brothers who asked for, I have never used transducers in any project so this is just my understanding from the official documentation plus some test.
If you see any error or mistake please let me know.
Transducers are not complex or difficult, you just need to visualize the flow of data.
Suppose we have a small sequence.
Now we want to increment of one the value of any element in the sequence and then remove the element that are odd.
The standard approach would look similar to this:
What is happening here ?
First every element in s
is incremented.
Then the element incremented are passed through a remove which remove every element that return true
to the predicate odd?
For sake of simplicity let’s call stuff like (map inc s)
or (remove odd? s)
with a simple name: transformation
.
Also let’s call flow
a composition of transformation.
Now let’s write the code above in a more clojurist way:
Now it is important to don’t get confused, ->>
is a macro, not a function.
The two codes above are exactly the same.
However the macro expression is pretty useful to visualize what is happening.
The sequence s
flow inside the transformation (map inc)
the result, then, flow inside the transformation (remove odd?)
and we get our output.
transducers are a way to create flow
combining different transformation
.
Without transducers you have to pass an input, a sequence, to our transformation, with transducer we don’t have this necessity anymore.
Also, function like map
, remove
, filter
create a lazy sequence each.
A flow like the one above will create 3 lazy sequence.
However if we use transducer
we don’t create the intermediate lazy sequences.
Also I can see transducer useful if we have a lot of different flow
that need to be composed.
If you have any question just let me know :)