Skip to content

Instantly share code, notes, and snippets.

@jaawerth
Last active October 17, 2020 08:08
Show Gist options
  • Save jaawerth/43347203f955e9f076bb to your computer and use it in GitHub Desktop.
Save jaawerth/43347203f955e9f076bb 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