Generic curry function with n-arguments in typescript
It isn't too bothersome to have to write curry2/curry1/curry3 everywhere instead of a single unified interface of curry,
You can with overloading (doc https://basarat.gitbooks.io/typescript/content/docs/types/functions.html)
More
Something to get you started:
function curry<T1,T2,R>(func:(arg1:T1, arg2:T2) => R, param2: T2):(arg:T1) => R;function curry<T1,T2,T3,R>(func:(arg1:T1, arg2:T2, arg3: T3) => R, param2: T2):(arg:T1) => R;function curry(){ // Implement return undefined;};
The example of basarat is quite good but the second overload does not seem to be correct.This is what I came up with, when writing a type signature for a curry function:
interface curry { <T1, T2, R>(func: (p1: T1, p2: T2) => R): (p: T1) => (p: T2) => R; <T1, T2, T3, R>(func: (p1: T1, p2: T2, p3: T3) => R): (p: T1) => (p: T2) => (p: T3) => R; <T1, T2, T3, T4, R>(func: (p1: T1, p2: T2, p3: T3, p4: T4) => R): (p: T1) => (p: T2) => (p: T3) => (p: T4) => R; <T1, T2, T3, T4, T5, R>(func: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5) => R): (p: T1) => (p: T2) => (p: T3) => (p: T4) => (p: T5) => R;}
And of course you can repeat it with more parameters until you reach a safe depth :)
// the actual curry function implementation ommitedvar makeCurry: curry = <any> null;// example function that we would like to curry, with two parametersvar getInfo = (age: number, name: string) => { return `${name} is ${age} years old`;}// the previous function curriedvar getInfoCurried = makeCurry<number, string, string>(getInfo);var info = getInfoCurried(26)('Gergo');