gnicholas
gnicholas

Reputation: 2077

Generic curry function with n-arguments in typescript

I can create a generic currying function for functions with a set number of arguments. IE)

function curry2<T1,T2,R>(func:(arg1:T1, arg2:T2) => R, param2: T2):(arg:T1) => R{
    return (param1:T1) => func(param1, param2);
};

However, I cannot find a (typesafe) way to implement a generic curry function for a function with any number of arguments. In a different language I would name all my currying functions (ie: curry1, curry2, curry3, etc) the same thing (curry) and then have function overloading do the work of running the correct implementation of curry. However, typescript does not allow function overloading like this.

It isn't too bothersome to have to write curry2/curry1/curry3 everywhere instead of a single unified interface of curry, but if there is a way to do it I would appreciate knowing how!

Upvotes: 7

Views: 2348

Answers (2)

gsanta
gsanta

Reputation: 774

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 ommited
var makeCurry: curry = <any> null;

// example function that we would like to curry, with two parameters
var getInfo = (age: number, name: string) => {
    return `${name} is ${age} years old`;
}

// the previous function curried
var getInfoCurried = makeCurry<number, string, string>(getInfo);

var info = getInfoCurried(26)('Gergo');

Upvotes: 1

basarat
basarat

Reputation: 275819

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;
};

Upvotes: 3

Related Questions