Skip to content

Commit ee9c68f

Browse files
committed
Test transformer protocol with transduce
1 parent 04e287b commit ee9c68f

File tree

4 files changed

+145
-15
lines changed

4 files changed

+145
-15
lines changed

__tests__/transformerProtocol.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
///<reference path='../resources/jest.d.ts'/>
2+
3+
import * as jasmineCheck from 'jasmine-check';
4+
import * as t from 'transducers-js';
5+
jasmineCheck.install();
6+
7+
import { List, Map, Set, Stack } from '../';
8+
9+
describe('Transformer Protocol', () => {
10+
11+
it('transduces Stack without initial values', () => {
12+
let s = Stack.of(1, 2, 3, 4);
13+
let xform = t.comp(
14+
t.filter(x => x % 2 === 0),
15+
t.map(x => x + 1),
16+
);
17+
let s2 = t.transduce(xform, Stack(), s);
18+
expect(s.toArray()).toEqual([1, 2, 3, 4]);
19+
expect(s2.toArray()).toEqual([5, 3]);
20+
});
21+
22+
it('transduces Stack with initial values', () => {
23+
let v1 = Stack.of(1, 2, 3);
24+
let v2 = Stack.of(4, 5, 6, 7);
25+
let xform = t.comp(
26+
t.filter(x => x % 2 === 0),
27+
t.map(x => x + 1),
28+
);
29+
let r = t.transduce(xform, Stack(), v1, v2);
30+
expect(v1.toArray()).toEqual([1, 2, 3]);
31+
expect(v2.toArray()).toEqual([4, 5, 6, 7]);
32+
expect(r.toArray()).toEqual([7, 5, 1, 2, 3]);
33+
});
34+
35+
it('transduces List without initial values', () => {
36+
let v = List.of(1, 2, 3, 4);
37+
let xform = t.comp(
38+
t.filter(x => x % 2 === 0),
39+
t.map(x => x + 1),
40+
);
41+
let r = t.transduce(xform, List(), v);
42+
expect(v.toArray()).toEqual([1, 2, 3, 4]);
43+
expect(r.toArray()).toEqual([3, 5]);
44+
});
45+
46+
it('transduces List with initial values', () => {
47+
let v1 = List.of(1, 2, 3);
48+
let v2 = List.of(4, 5, 6, 7);
49+
let xform = t.comp(
50+
t.filter(x => x % 2 === 0),
51+
t.map(x => x + 1),
52+
);
53+
let r = t.transduce(xform, List(), v1, v2);
54+
expect(v1.toArray()).toEqual([1, 2, 3]);
55+
expect(v2.toArray()).toEqual([4, 5, 6, 7]);
56+
expect(r.toArray()).toEqual([1, 2, 3, 5, 7]);
57+
});
58+
59+
it('transduces Map without initial values', () => {
60+
let m1 = Map({a: 1, b: 2, c: 3, d: 4});
61+
let xform = t.comp(
62+
t.filter(([k, v]) => v % 2 === 0),
63+
t.map(([k, v]) => [k, v * 2]),
64+
);
65+
let m2 = t.transduce(xform, Map(), m1);
66+
expect(m1.toObject()).toEqual({a: 1, b: 2, c: 3, d: 4});
67+
expect(m2.toObject()).toEqual({b: 4, d: 8});
68+
});
69+
70+
it('transduces Map with initial values', () => {
71+
let m1 = Map({a: 1, b: 2, c: 3});
72+
let m2 = Map({a: 4, b: 5});
73+
let xform = t.comp(
74+
t.filter(([k, v]) => v % 2 === 0),
75+
t.map(([k, v]) => [k, v * 2]),
76+
);
77+
let m3 = t.transduce(xform, Map(), m1, m2);
78+
expect(m1.toObject()).toEqual({a: 1, b: 2, c: 3});
79+
expect(m2.toObject()).toEqual({a: 4, b: 5});
80+
expect(m3.toObject()).toEqual({a: 8, b: 2, c: 3});
81+
});
82+
83+
it('transduces Set without initial values', () => {
84+
let s1 = Set.of(1, 2, 3, 4);
85+
let xform = t.comp(
86+
t.filter(x => x % 2 === 0),
87+
t.map(x => x + 1),
88+
);
89+
let s2 = t.transduce(xform, Set(), s1);
90+
expect(s1.toArray()).toEqual([1, 2, 3, 4]);
91+
expect(s2.toArray()).toEqual([3, 5]);
92+
});
93+
94+
it('transduces Set with initial values', () => {
95+
let s1 = Set.of(1, 2, 3, 4);
96+
let s2 = Set.of(2, 3, 4, 5, 6);
97+
let xform = t.comp(
98+
t.filter(x => x % 2 === 0),
99+
t.map(x => x + 1),
100+
);
101+
let s3 = t.transduce(xform, Set(), s1, s2);
102+
expect(s1.toArray()).toEqual([1, 2, 3, 4]);
103+
expect(s2.toArray()).toEqual([2, 3, 4, 5, 6]);
104+
expect(s3.toArray()).toEqual([1, 2, 3, 4, 5, 7]);
105+
});
106+
107+
});

dist/immutable.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,13 @@ MapPrototype[IS_MAP_SENTINEL] = true;
21862186
MapPrototype[DELETE] = MapPrototype.remove;
21872187
MapPrototype.removeIn = MapPrototype.deleteIn;
21882188
MapPrototype.removeAll = MapPrototype.deleteAll;
2189+
MapPrototype['@@transducer/init'] = MapPrototype.asMutable;
2190+
MapPrototype['@@transducer/step'] = function(result, arr) {
2191+
return result.set(arr[0], arr[1]);
2192+
};
2193+
MapPrototype['@@transducer/result'] = function(obj) {
2194+
return obj.asImmutable();
2195+
};
21892196

21902197
// #pragma Trie Nodes
21912198

@@ -3118,6 +3125,11 @@ ListPrototype.withMutations = MapPrototype.withMutations;
31183125
ListPrototype.asMutable = MapPrototype.asMutable;
31193126
ListPrototype.asImmutable = MapPrototype.asImmutable;
31203127
ListPrototype.wasAltered = MapPrototype.wasAltered;
3128+
ListPrototype['@@transducer/init'] = ListPrototype.asMutable;
3129+
ListPrototype['@@transducer/step'] = function(result, arr) {
3130+
return result.push(arr);
3131+
};
3132+
ListPrototype['@@transducer/result'] = MapPrototype['@@transducer/result'];
31213133

31223134
var VNode = function VNode(array, ownerID) {
31233135
this.array = array;
@@ -3912,6 +3924,11 @@ StackPrototype.wasAltered = MapPrototype.wasAltered;
39123924
StackPrototype.shift = StackPrototype.pop;
39133925
StackPrototype.unshift = StackPrototype.push;
39143926
StackPrototype.unshiftAll = StackPrototype.pushAll;
3927+
StackPrototype['@@transducer/init'] = StackPrototype.asMutable;
3928+
StackPrototype['@@transducer/step'] = function(result, arr) {
3929+
return result.unshift(arr);
3930+
};
3931+
StackPrototype['@@transducer/result'] = MapPrototype['@@transducer/result'];
39153932

39163933
function makeStack(size, head, ownerID, hash) {
39173934
var map = Object.create(StackPrototype);
@@ -4197,6 +4214,11 @@ SetPrototype.mergeDeepWith = SetPrototype.mergeWith;
41974214
SetPrototype.withMutations = MapPrototype.withMutations;
41984215
SetPrototype.asMutable = MapPrototype.asMutable;
41994216
SetPrototype.asImmutable = MapPrototype.asImmutable;
4217+
SetPrototype['@@transducer/init'] = SetPrototype.asMutable;
4218+
SetPrototype['@@transducer/step'] = function(result, arr) {
4219+
return result.add(arr);
4220+
};
4221+
SetPrototype['@@transducer/result'] = MapPrototype['@@transducer/result'];
42004222

42014223
SetPrototype.__empty = emptySet;
42024224
SetPrototype.__make = makeSet;

0 commit comments

Comments
 (0)