Skip to content

Instantly share code, notes, and snippets.

@mikeparisstuff
Last active May 12, 2017 03:35
Show Gist options
  • Save mikeparisstuff/f5cdee0fe7d5ed4e6a2be348b81eac12 to your computer and use it in GitHub Desktop.
Save mikeparisstuff/f5cdee0fe7d5ed4e6a2be348b81eac12 to your computer and use it in GitHub Desktop.
A simple functional compose implementation in javascript.
export function compose(...funcs: Array<(...args: Array<any>) => any>) {
if (funcs.length === 0) {
return (arg: any) => arg;
}
if (funcs.length === 1) {
return funcs[0];
}
const last = funcs[funcs.length - 1];
return (...args: Array<any>) => {
let result = last(...args);
for (let index = funcs.length - 2; index >= 0; index--) {
const fn = funcs[index];
result = fn(result);
}
return result;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment