Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Last active March 27, 2017 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shuumatsu/d6f7e6928c791373a2c9e82c17d6fc1d to your computer and use it in GitHub Desktop.
Save Shuumatsu/d6f7e6928c791373a2c9e82c17d6fc1d to your computer and use it in GitHub Desktop.
/**
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for
* the resulting composite function.
*
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (...args) => f(g(h(...args))).
*/
export default function compose(...funcs) {
if (funcs.length === 0) {
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