Last active
October 17, 2020 08:08
-
-
Save jaawerth/43347203f955e9f076bb to your computer and use it in GitHub Desktop.
ES5 and ES6 implementations of compose - readable implementation, not optimized
This file contains 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 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