Skip to content

Commit

Permalink
refactor(lib): add Curry
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Oct 15, 2016
1 parent 5f75a07 commit 300113c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/Curry.ts
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)
}
}
27 changes: 27 additions & 0 deletions test/test.Curry.ts
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]
)
})

0 comments on commit 300113c

Please sign in to comment.