Created
October 4, 2017 01:15
-
-
Save cyan33/7c4829a66360042f77b457dde2b4a453 to your computer and use it in GitHub Desktop.
chain functions compose
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
/* | |
* I got this inspiration from the source code of redux. | |
* Essentially, compose(f, g, h) equals to (arg) => f(g(h(arg))) | |
*/ | |
function compose(...funcs) { | |
if (!funcs.length) return (arg) => arg | |
if (funcs.length === 1) return funcs[0] | |
return funcs.reduce((a, b) => (...args) => a(b(...args))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment