Composition utility
What if I wanted to make a compose utility that composes two functions based on some assumption on what these function accept as parameters and return values?
function compose2(fn1, fn2) {
return function comp(){
var args = [].slice.call(arguments)l
return fn2(
fn1(args.shift(), args.shift()),
args.shift()
);
}
}
var multAndSum = compos2(mult,sum);
multAndSum(3,4,5); //17
The above "crappy" code ([].slice.call(arguments)
) is something we have to do, turn the arguments array into a real array. args.shift()
is just to retrieve elements from the arguments list to pass to the function. There are big assumption about the number of functions and number of arguments and how they are passed.
In functional programming, you try to determine the most common pattern and create compositions for them. This is a real rabbit hole, that is not useful for now to go into.