Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created November 11, 2017 05:14
Show Gist options
  • Save rauschma/985d6fda6227bed5599f5f67764d6933 to your computer and use it in GitHub Desktop.
Save rauschma/985d6fda6227bed5599f5f67764d6933 to your computer and use it in GitHub Desktop.
function curry2(func) {
return function (...args) {
if (args.length < 2) {
return func.bind(this, ...args);
}
return func.apply(this, args);
};
}
@rauschma
Copy link
Author

@scyclow
Copy link

scyclow commented Nov 11, 2017

// @flow
function curry2<M: Array<mixed>, T>(
  func: (...M) => T
): (...M) => (T | (...M) => T) {
  return function (...args) {
    if (args.length < 2) {
        return func.bind(this, ...args);
    }
    return func.apply(this, args);
  };    
}

🤷‍♂️

@aminpaks
Copy link

aminpaks commented Nov 11, 2017

[email protected] here is the example

function curry2<R>(func: (...args: any[]) => R ): () => R {
    return function (...args: any[]): R {
        if (args.length < 2) {
            return func.bind(this, ...args);
        }

        return func.apply(this, args);
    }
}

typescript@next once variadic kinds are available

function curry2<...T, R>(func: (...args: ...T) => R ): (...args: ...T) => R {
    return function (...args: ...T): R {
        if (args.length < 2) {
            return func.bind(this, ...args);
        }

        return func.apply(this, args);
    }
}

@rauschma
Copy link
Author

@scyclow @aminpaks Thanks! I’ll experiment with assuming that func has exactly two parameters (= 3 type variables, incl. the result).

@aminpaks
Copy link

aminpaks commented Nov 11, 2017

assuming that func has exactly two parameters (= 3 type variables, incl. the result)

@rauschma That would make it much easier with the current implementation of Typescript:

function curry2<TF, TS, R>(func: (first: TF, second: TS) => R ): (first: TF, second: TS) => R {
    return function (first: TF, second: TS): R {
        return func.call(this, first, second);
    }
}

Example

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