Conveniently composes multiple functions.
Conveniently composes multiple functions.
Because compose
is limited to two functions, composing more than that
is awkward:
const compose = require('folktale/core/lambda/compose')
const inc = (x) => x + 1;
const double = (x) => x * 2;
const square = (x) => x * x;
const incDoubleSquare = compose(inc, compose(double, square));
incDoubleSquare(3);
// ==> 19
In these cases one may use compose.all
, which is a variadic convenience
for composing multiple functions:
const incDoubleSquare2 = compose.all(inc, double, square);
incDoubleSquare2(3);
// ==> 19
function(...fns) {
/* eslint-disable no-magic-numbers */
if (fns.length < 1) { // eslint-disable-next-line prefer-rest-params
throw new TypeError(`compose.all requires at least one argument, ${arguments.length} given.`);
}
return fns.reduce(compose);
}