Skip to content

Commit

Permalink
Merge pull request #28 from tusharmath/currying
Browse files Browse the repository at this point in the history
Currying
  • Loading branch information
tusharmath authored Oct 15, 2016
2 parents 5f75a07 + 9a52465 commit 0a711a2
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/lib/Curry.ts
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)
}
}
33 changes: 23 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@
export {Observer} from './lib/Observer'

// Operators
export {filter} from './operators/Filter'
export {join} from './operators/Join'
export {map} from './operators/Map'
export {scan} from './operators/Scan'
export {slice} from './operators/Slice'
export {tap} from './operators/Tap'
export {reduce} from './operators/Reduce'
import {filter as _filter} from './operators/Filter'
import {join as _join} from './operators/Join'
import {map as _map} from './operators/Map'
import {scan as _scan} from './operators/Scan'
import {slice as _slice} from './operators/Slice'
import {tap as _tap} from './operators/Tap'
import {reduce as _reduce} from './operators/Reduce'

// Sources
export {fromArray} from './sources/FromArray'
export {interval} from './sources/Interval'
export {fromDOM} from './sources/FromDOM'
import {fromArray as _fromArray} from './sources/FromArray'
import {interval as _interval} from './sources/Interval'
import {fromDOM as _fromDOM} from './sources/FromDOM'
import {Curry} from './lib/Curry'


export const filter = Curry(_filter)
export const join = Curry(_join)
export const map = Curry(_map)
export const scan = Curry(_scan)
export const slice = Curry(_slice)
export const tap = Curry(_tap)
export const reduce = Curry(_reduce)
export const fromArray = Curry(_fromArray)
export const interval = Curry(_interval)
export const fromDOM = Curry(_fromDOM)
7 changes: 7 additions & 0 deletions src/types/ICurryFunction.ts
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>
}
28 changes: 28 additions & 0 deletions test/test.Curry.ts
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]
)
})
3 changes: 2 additions & 1 deletion test/test.FromArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
'use strict'

import test from 'ava'
import {fromArray, map} from '../src/main'
import {TestScheduler} from '../src/testing/TestScheduler'
import {ReactiveEvents} from '../src/testing/ReactiveEvents'
import {map} from '../src/operators/Map'
import {fromArray} from '../src/sources/FromArray'

const {next, error} = ReactiveEvents
test(t => {
Expand Down
2 changes: 1 addition & 1 deletion test/test.IntervalObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
'use strict'

import test from 'ava'
import {interval} from '../src/main'
import {TestScheduler} from '../src/testing/TestScheduler'
import {ReactiveEvents, EventError} from '../src/testing/ReactiveEvents'
import {IEvent} from '../src/types/IEvent'
import {interval} from '../src/sources/Interval'
const {next, error} = ReactiveEvents

test('subscribe()', t => {
Expand Down
2 changes: 1 addition & 1 deletion test/test.JoinObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/

import test from 'ava'
import {join} from '../src/main'
import {TestScheduler} from '../src/testing/TestScheduler'
import {ReactiveEvents} from '../src/testing/ReactiveEvents'
import {join} from '../src/operators/Join'
const {next, complete} = ReactiveEvents

test('subscribe()', t => {
Expand Down
2 changes: 1 addition & 1 deletion test/test.MapObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
'use strict'

import test from 'ava'
import {map} from '../src/main'
import {TestScheduler} from '../src/testing/TestScheduler'
import {ReactiveEvents} from '../src/testing/ReactiveEvents'
import {map} from '../src/operators/Map'

const {next, complete} = ReactiveEvents

Expand Down
2 changes: 1 addition & 1 deletion test/test.ScanObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/

import test from 'ava'
import {scan} from '../src/main'
import {TestScheduler} from '../src/testing/TestScheduler'
import {ReactiveEvents} from '../src/testing/ReactiveEvents'
import {scan} from '../src/operators/Scan'

const {next, complete} = ReactiveEvents

Expand Down
2 changes: 1 addition & 1 deletion test/test.SliceObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import test from 'ava'
import {TestScheduler} from '../src/testing/TestScheduler'
import {slice} from '../src/main'
import {ReactiveEvents} from '../src/testing/ReactiveEvents'
import {slice} from '../src/operators/Slice'

const {next, complete} = ReactiveEvents
test('takeN(0, 3)', t => {
Expand Down

0 comments on commit 0a711a2

Please sign in to comment.