From 300113ce15d2d1839ca04b8f3c5ea09d5572471d Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Sat, 15 Oct 2016 21:33:01 +0530 Subject: [PATCH 1/3] refactor(lib): add Curry --- src/lib/Curry.ts | 15 +++++++++++++++ test/test.Curry.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/lib/Curry.ts create mode 100644 test/test.Curry.ts diff --git a/src/lib/Curry.ts b/src/lib/Curry.ts new file mode 100644 index 0000000..2ec575b --- /dev/null +++ b/src/lib/Curry.ts @@ -0,0 +1,15 @@ +/** + * Created by tushar.mathur on 15/10/16. + */ + +export interface ICurryFunction { + (...k: any[]): T | ICurryFunction +} + +export function Curry (f: ICurryFunction): ICurryFunction { + return function curried (...t: any[]): T | ICurryFunction { + if (t.length === 0) return curried + if (t.length === f.length) return f(...t) + return curried.bind(this, ...t) + } +} diff --git a/test/test.Curry.ts b/test/test.Curry.ts new file mode 100644 index 0000000..0879f2f --- /dev/null +++ b/test/test.Curry.ts @@ -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( + (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 + t.deepEqual(func2(3), [1, 2, 3]) +}) + +test('func(1)(2)(3)', t => { + t.deepEqual(( + (func(1) as ICurryFunction)(2) as ICurryFunction)(3), + [1, 2, 3] + ) +}) From d9d12b0635c38852e3b0930da5849bd330247ce6 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Sat, 15 Oct 2016 22:19:23 +0530 Subject: [PATCH 2/3] refactor(curry): move interface to types folder --- src/lib/Curry.ts | 5 ++--- src/types/ICurryFunction.ts | 7 +++++++ test/test.Curry.ts | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 src/types/ICurryFunction.ts diff --git a/src/lib/Curry.ts b/src/lib/Curry.ts index 2ec575b..6946b66 100644 --- a/src/lib/Curry.ts +++ b/src/lib/Curry.ts @@ -2,9 +2,8 @@ * Created by tushar.mathur on 15/10/16. */ -export interface ICurryFunction { - (...k: any[]): T | ICurryFunction -} + +import {ICurryFunction} from '../types/ICurryFunction' export function Curry (f: ICurryFunction): ICurryFunction { return function curried (...t: any[]): T | ICurryFunction { diff --git a/src/types/ICurryFunction.ts b/src/types/ICurryFunction.ts new file mode 100644 index 0000000..6f7a66c --- /dev/null +++ b/src/types/ICurryFunction.ts @@ -0,0 +1,7 @@ +/** + * Created by tushar.mathur on 15/10/16. + */ + +export interface ICurryFunction { + (...k: any[]): T | ICurryFunction +} diff --git a/test/test.Curry.ts b/test/test.Curry.ts index 0879f2f..4ed65eb 100644 --- a/test/test.Curry.ts +++ b/test/test.Curry.ts @@ -3,7 +3,8 @@ */ import test from 'ava' -import {Curry, ICurryFunction} from '../src/lib/Curry' +import {Curry} from '../src/lib/Curry' +import {ICurryFunction} from '../src/types/ICurryFunction' const func = Curry( (a: number, b: number, c: number) => [a, b, c] From 9a52465b6af4b1cdc603df5a066148e38f2f6929 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Sat, 15 Oct 2016 22:23:26 +0530 Subject: [PATCH 3/3] feat(main): export operators and sources as curried by default --- src/main.ts | 33 +++++++++++++++++++++++---------- test/test.FromArray.ts | 3 ++- test/test.IntervalObservable.ts | 2 +- test/test.JoinObservable.ts | 2 +- test/test.MapObservable.ts | 2 +- test/test.ScanObservable.ts | 2 +- test/test.SliceObservable.ts | 2 +- 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index 60ffe58..5471e89 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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) diff --git a/test/test.FromArray.ts b/test/test.FromArray.ts index cb7f1cc..8dd2894 100644 --- a/test/test.FromArray.ts +++ b/test/test.FromArray.ts @@ -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 => { diff --git a/test/test.IntervalObservable.ts b/test/test.IntervalObservable.ts index a66e7cb..513e2c1 100644 --- a/test/test.IntervalObservable.ts +++ b/test/test.IntervalObservable.ts @@ -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 => { diff --git a/test/test.JoinObservable.ts b/test/test.JoinObservable.ts index 3fce906..23d8f08 100644 --- a/test/test.JoinObservable.ts +++ b/test/test.JoinObservable.ts @@ -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 => { diff --git a/test/test.MapObservable.ts b/test/test.MapObservable.ts index 7430f4b..a550c13 100644 --- a/test/test.MapObservable.ts +++ b/test/test.MapObservable.ts @@ -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 diff --git a/test/test.ScanObservable.ts b/test/test.ScanObservable.ts index bc98ece..5c952f4 100644 --- a/test/test.ScanObservable.ts +++ b/test/test.ScanObservable.ts @@ -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 diff --git a/test/test.SliceObservable.ts b/test/test.SliceObservable.ts index be5c6f4..9f6ec2f 100644 --- a/test/test.SliceObservable.ts +++ b/test/test.SliceObservable.ts @@ -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 => {