Last active
May 12, 2017 03:35
-
-
Save mikeparisstuff/f5cdee0fe7d5ed4e6a2be348b81eac12 to your computer and use it in GitHub Desktop.
A simple functional compose implementation in javascript.
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
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