Created
November 11, 2017 05:14
-
-
Save rauschma/985d6fda6227bed5599f5f67764d6933 to your computer and use it in GitHub Desktop.
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
function curry2(func) { | |
return function (...args) { | |
if (args.length < 2) { | |
return func.bind(this, ...args); | |
} | |
return func.apply(this, args); | |
}; | |
} |
// @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);
};
}
🤷♂️
[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);
}
}
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);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks good: http://2ality.com/2017/11/currying-in-js.html#comment-3610735527