Skip to content

Instantly share code, notes, and snippets.

@typoerr
Last active June 25, 2017 23:44
Show Gist options
  • Select an option

  • Save typoerr/cd9214abcbbd034de62dd00c4f555fb5 to your computer and use it in GitHub Desktop.

Select an option

Save typoerr/cd9214abcbbd034de62dd00c4f555fb5 to your computer and use it in GitHub Desktop.
interface CurriedFunction2<A, B, C> {
(): CurriedFunction2<A, B, C>;
(a: A): (b: B) => C;
(a: A, b: B): C;
}
interface CurriedFunction3<A, B, C, D> {
(): CurriedFunction3<A, B, C, D>;
(a: A): CurriedFunction2<B, C, D>;
(a: A, b: B): (c: C) => D;
(a: A, b: B, c: C): D;
}
export function curry2<A, B, C>(f: (a: A, b: B) => C) {
function curried(a: A, b: B) {
switch (arguments.length) {
case 0: return curried;
case 1: return (b: B) => f(a, b);
default: return f(a, b);
}
}
return curried as CurriedFunction2<A, B, C>;
}
export function curry3<A, B, C, D>(f: (a: A, b: B, c: C) => D) {
function curried(a: A, b: B, c: C): any {
switch (arguments.length) {
case 0: return curried;
case 1: return curry2((_b: B, c: C) => f(a, b, c));
case 2: return (c: C) => f(a, b, c);
default: return f(a, b, c);
}
}
return curried as CurriedFunction3<A, B, C, D>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment