Skip to content

Instantly share code, notes, and snippets.

@alpenzoo
Forked from jaawerth/compose.es6.js
Created April 25, 2019 14:05
Show Gist options
  • Save alpenzoo/bc856886e99f60de7c387214a03c8271 to your computer and use it in GitHub Desktop.
Save alpenzoo/bc856886e99f60de7c387214a03c8271 to your computer and use it in GitHub Desktop.
ES5 and ES6 implementations of compose - readable implementation, not optimized
function compose(...fnArgs) {
const [first, ...funcs] = fnArgs.reverse();
return function(...args) {
return funcs.reduce((res, fn) => fn(res), first(...args));
};
}
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