This afternoon, I tried implement myself a compose function follow this:
function compose(...fns) {
return function composed(result){
return fns.reverse().reduce( function reducer(result,fn){
return fn( result );
}, result );
};
}
// or the ES6 => form
var compose = (...fns) =>
result =>
fns.reverse().reduce(
(result,fn) =>
fn( result )
, result
);
I found that this compose function always give me wrong result. The reason is that reverse function mutates the fns array and our fns array is kept in closure.
This afternoon, I tried implement myself a
compose
function follow this:I found that this
compose
function always give me wrong result. The reason is thatreverse
function mutates thefns
array and ourfns
array is kept in closure.Here is my code snippet:
Stack trace when run the
computeTaxForIncome
function:My suggestion is adding
slice
function to the implementation to avoid mutating thefns
array.I will make a pull request for this issue when I have free time.