January 2020
Intermediate to advanced
470 pages
11h 13m
English
To be able to work with all kinds of reducers and produce whatever kind of result they build, we'll need to make a small change. The idea is simple: let's modify our makeReducer() function so that it will accept a final reducer and a starting value for the accumulator:
const makeReducer2 = (arr, fns, reducer = addToArray, initial = []) => arr.reduce(compose(...fns)(reducer), initial);const a3 = makeReducer2(myArray, [ testOddR, duplicateR, testUnderFiftyR, addThreeR,]);/*[ 21, 25 ]*/
To make this function more usable, we specified our array-building function (and [] as a starting accumulator value) so that if you skip those two parameters, you'll get a reducer that produces an array. Now, let's look at the other ...
Read now
Unlock full access