-
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.
Merge pull request #28 from tusharmath/currying
Currying
- Loading branch information
Showing
10 changed files
with
79 additions
and
16 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,14 @@ | ||
/** | ||
* Created by tushar.mathur on 15/10/16. | ||
*/ | ||
|
||
|
||
import {ICurryFunction} from '../types/ICurryFunction' | ||
|
||
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
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,7 @@ | ||
/** | ||
* Created by tushar.mathur on 15/10/16. | ||
*/ | ||
|
||
export interface ICurryFunction<T> { | ||
(...k: any[]): T | ICurryFunction<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,28 @@ | ||
/** | ||
* Created by tushar.mathur on 15/10/16. | ||
*/ | ||
|
||
import test from 'ava' | ||
import {Curry} from '../src/lib/Curry' | ||
import {ICurryFunction} from '../src/types/ICurryFunction' | ||
|
||
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] | ||
) | ||
}) |
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
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
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
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
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
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