-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f75a07
commit 300113c
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Created by tushar.mathur on 15/10/16. | ||
*/ | ||
|
||
export interface ICurryFunction<T> { | ||
(...k: any[]): T | ICurryFunction<T> | ||
} | ||
|
||
export function Curry <T, R> (f: ICurryFunction<T>): ICurryFunction<T> { | ||
return function curried (...t: any[]): T | ICurryFunction<T> { | ||
if (t.length === 0) return curried | ||
if (t.length === f.length) return f(...t) | ||
return curried.bind(this, ...t) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Created by tushar.mathur on 15/10/16. | ||
*/ | ||
|
||
import test from 'ava' | ||
import {Curry, ICurryFunction} from '../src/lib/Curry' | ||
|
||
const func = Curry<number[], number>( | ||
(a: number, b: number, c: number) => [a, b, c] | ||
) | ||
|
||
|
||
test('func(1, 2, 3)', t => { | ||
t.deepEqual(func(1, 2, 3), [1, 2, 3]) | ||
}) | ||
|
||
test('func(1, 2)(3)', t => { | ||
const func2 = func(1, 2) as ICurryFunction<number[]> | ||
t.deepEqual(func2(3), [1, 2, 3]) | ||
}) | ||
|
||
test('func(1)(2)(3)', t => { | ||
t.deepEqual(( | ||
(func(1) as ICurryFunction<number[]>)(2) as ICurryFunction<number[]>)(3), | ||
[1, 2, 3] | ||
) | ||
}) |