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…
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.