Last active
June 25, 2017 23:44
-
-
Save typoerr/cd9214abcbbd034de62dd00c4f555fb5 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
| 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