Skip to content

Instantly share code, notes, and snippets.

@nodkz
Created June 15, 2016 07:09
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 nodkz/73b05c2bbbe0c7e7538c7981ad75b56d to your computer and use it in GitHub Desktop.
Save nodkz/73b05c2bbbe0c7e7538c7981ad75b56d to your computer and use it in GitHub Desktop.
Help to annotate this function with Flow
/**
* 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;
}
const last = funcs[funcs.length - 1];
const rest = funcs.slice(0, -1);
return (...args) => rest.reduceRight((composed, f) => f(composed), last(...args));
}
@nodkz
Copy link
Author

nodkz commented Jun 16, 2016

Solution can be found here facebook/flow#1950 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment