Skip to content

Instantly share code, notes, and snippets.

@cyan33
Created October 4, 2017 01:15
Show Gist options
  • Save cyan33/7c4829a66360042f77b457dde2b4a453 to your computer and use it in GitHub Desktop.
Save cyan33/7c4829a66360042f77b457dde2b4a453 to your computer and use it in GitHub Desktop.
chain functions compose
/*
* 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