Skip to content

Instantly share code, notes, and snippets.

@nickytonline
Last active December 8, 2015 02:11
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 nickytonline/a881b2bc576ee2659aa4 to your computer and use it in GitHub Desktop.
Save nickytonline/a881b2bc576ee2659aa4 to your computer and use it in GitHub Desktop.
Messing around with functional progamming in TypeScript
export default function compose<T>(...functions: { (x: T): T }[]) {
return x => functions.reduce((previous, current) => x => current(previous(x)))(x);
}
/* usage:
const composed = compose<number>(x => x * 2,x => x * 3, x => x * 4);
console.log(composed(4)) //96
or
const composed = compose<string>(x=>x.toUpperCase(),x=>x+' :)')
console.log(`Hello ${composed('John Doe')}`); // Hello JOHN DOE :)
*/
@nickytonline
Copy link
Author

An implementation of the compose function in TypeScript.

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