function composition in javascript code example
Example 1: javascript compose function
const compose = (...funcs) => args => funcs.reduceRight((arg, fn) => fn(arg), args);
// Or if you like in ES5
function compose(...funcs)
{
return function(args)
{
return funcs.reduceRight( (arg, fn) => fn(arg), args);
}
}
Example 2: what is functional composition
const myResult = f1( f2(data) ); // composition: pass the output DATA of one function to the input of another ... like pipes.
function add(a){return function(b){return a + b} } // functional composition
add(2)(3) //-> 5