Last active
December 8, 2015 02:11
-
-
Save nickytonline/a881b2bc576ee2659aa4 to your computer and use it in GitHub Desktop.
Messing around with functional progamming in TypeScript
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 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 :) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An implementation of the compose function in TypeScript.