-
-
Save alpenzoo/bc856886e99f60de7c387214a03c8271 to your computer and use it in GitHub Desktop.
ES5 and ES6 implementations of compose - readable implementation, not optimized
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compose(...fnArgs) { | |
const [first, ...funcs] = fnArgs.reverse(); | |
return function(...args) { | |
return funcs.reduce((res, fn) => fn(res), first(...args)); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compose() { | |
var funcs = Array.prototype.slice.call(arguments).reverse(); // turn args into (reversed) array | |
return function() { | |
return funcs.slice(1).reduce(function(res, fn) { | |
return fn(res); | |
}, funcs[0].apply(undefined, arguments)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment