Skip to content

Instantly share code, notes, and snippets.

@tejacques
Created December 12, 2015 01:07
Show Gist options
  • Save tejacques/72c7e0bcc508cdf107ae to your computer and use it in GitHub Desktop.
Save tejacques/72c7e0bcc508cdf107ae to your computer and use it in GitHub Desktop.
function identity(x) {
return x;
}
function fp<T1, T2> (
transform: (x: T1) => T2
): (x: T1) => T2;
function fp<T1, T2, T3> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3
): (x: T1) => T3;
function fp<T1, T2, T3, T4> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3,
transform3: (x: T3) => T4
): (x: T1) => T4;
function fp<T1, T2, T3, T4, T5> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3,
transform3: (x: T3) => T4,
transform4: (x: T4) => T5
): (x: T1) => T5;
function fp<T1, T2, T3, T4, T5> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3,
transform3: (x: T3) => T4,
transform4: (x: T4) => T5
): (x: T1) => T5;
function fp<T1, T2, T3, T4, T5, T6> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3,
transform3: (x: T3) => T4,
transform4: (x: T4) => T5,
transform5: (x: T5) => T6
): (x: T1) => T6;
function fp<T1, T2, T3, T4, T5, T6, T7> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3,
transform3: (x: T3) => T4,
transform4: (x: T4) => T5,
transform5: (x: T5) => T6,
transform6: (x: T6) => T7
): (x: T1) => T7;
function fp<T1, T2, T3, T4, T5, T6, T7, T8> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3,
transform3: (x: T3) => T4,
transform4: (x: T4) => T5,
transform5: (x: T5) => T6,
transform6: (x: T6) => T7,
transform7: (x: T7) => T8
): (x: T1) => T8;
function fp<T1, T2, T3, T4, T5, T6, T7, T8, T9> (
transform1: (x: T1) => T2,
transform2: (x: T2) => T3,
transform3: (x: T3) => T4,
transform4: (x: T4) => T5,
transform5: (x: T5) => T6,
transform6: (x: T6) => T7,
transform7: (x: T7) => T8,
transform8: (x: T8) => T9
): (x: T1) => T9;
function fp(...transforms: Array<(x: any) => any>) {
switch (transforms.length) {
case 0: return identity;
case 1:
let transform = transforms[0];
return transform;
case 2:
let transform0 = transforms[0];
let transform1 = transforms[1];
return (value) => transform1(transform0(value))
default:
return (value) => {
let data = value;
for(let i = 0, len = transforms.length; i < len; i++) {
data = transform(data);
}
return data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment