Friday, 23 March, 2018 UTC


Summary

  • This opens up huge possibilities for composition and reuse of small reducer functions.
  • Great, we used decorator functions to wrap our reducers.
  • Great, now we have a chain of function calls, but we still can’t compose our reducers!
  • Let’s update our function so it would also accept reducer as an argument.
  • Let’s rewrite our mapping reducer as well: – – Also we need to add a final reducer that will push values to array for us: – – Now we can combine our mapping reducer and filtering transducer and do our calculations in one run.
Sweet chunk of functional paradigm for you today. I don’t know why did I write “versus” while they compliment each other. Anyway, let’s get to the good stuff…
@palashv2: Reducers vs Transducers: (Maksim promises a “sweet chunk of functional paradigm for you”…
Sweet chunk of functional paradigm for you today. I don’t know why did I write “versus” while they compliment each other. Anyway, let’s get to the good stuff…
Simply speaking a is a function that takes an accumulation and a value, and then returns a new accumulation.
You are already familiar with reducers if you’ve used the method. The function itself is not a reducer! It iterates over a collection and passes values to it’s “callback” that is a reducer here.
Let’s imagine that we have an array with five numbers: and we want to find the biggest of them.
The arrow function here is a reducer. The method only takes the result of previous reduction and calls the reducer with it and next element of an array.
Reducers can work with any kinds of values. The only rule is that the accumulation you return should have the same type that the accumulation you pass in.
For example you can easily create a reducer that will work with strings:
Actually it’s better to illustrate without method. Look:
The other cool thing about reducers is that you can chain them to perform a series of operations on some data. This opens up huge possibilities for composition and reuse of small reducer functions.
Reducers VS Transducers