Examining the compose function (borrowed from Redux)
function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
const double = x => x * 2
const square = x => x * x
const double1 = x => x * 3
compose(double, square, double1)(5)
In the final return statement
funcs.reduce((a, b) => (...args) => a(b(...args)))
Why is a function that takes ..args
being returned, instead of just
funcs.reduce((a, b) => a(b(...args))) ?