forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransduce.js
88 lines (77 loc) · 3.81 KB
/
transduce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('transduce', function() {
var add = R.add;
var mult = function(a, b) {return a * b;};
var isOdd = function(b) { return b % 2 !== 0; };
var isEven = function(b) { return b % 2 === 0; };
var square = function(a) {return a * a;};
var negate = function(a) {return -1 * a;};
var addxf = {
'@@transducer/step': function(acc, x) { return acc + x; },
'@@transducer/init': function() { return 0; },
'@@transducer/result': function(x) { return x; }
};
var listxf = {
'@@transducer/step': function(acc, x) { return acc.concat([x]); },
'@@transducer/init': function() { return []; },
'@@transducer/result': function(x) { return x; }
};
var multxf = {
'@@transducer/step': function(acc, x) { return acc * x; },
'@@transducer/init': function() { return 1; },
'@@transducer/result': function(x) { return x; }
};
var toxf = function(fn) {
return function(xf) {
return {
f: fn,
'@@transducer/step': xf['@@transducer/step'],
'@@transducer/result': xf['@@transducer/result'],
xf: xf
};
};
};
it('transduces into arrays', function() {
eq(R.transduce(R.map(add(1)), R.flip(R.append), [], [1, 2, 3, 4]), [2, 3, 4, 5]);
eq(R.transduce(R.filter(isOdd), R.flip(R.append), [], [1, 2, 3, 4]), [1, 3]);
eq(R.transduce(R.compose(R.map(add(1)), R.take(2)), R.flip(R.append), [], [1, 2, 3, 4]), [2, 3]);
eq(R.transduce(R.compose(R.filter(isOdd), R.take(1)), R.flip(R.append), [], [1, 2, 3, 4]), [1]);
eq(R.transduce(R.compose(R.uniq, R.map(square), R.take(3)), R.flip(R.append), [], [1, 1, 2, 2, 3, 3, 4, 4]), [1, 4, 9]);
eq(R.transduce(R.compose(R.filter(isEven), R.uniq), R.flip(R.append), [], [1, 1, 2, 2, 3, 3, 4, 4]), [2, 4]);
eq(R.transduce(R.compose(R.map(negate), R.uniqBy(Math.abs), R.map(square)), R.flip(R.append), [], [-1, -5, 2, 10, 1, 2]), [1, 25, 4, 100]);
eq(R.transduce(R.compose(R.uniqWith(R.eqBy(String)), R.map(square), R.take(3)), R.flip(R.append), [], [1, '1', '2', 2, 3, '3', '4', 4]), [1, 4, 9]);
});
it('transduces into strings', function() {
var _add = function(x, y) { return x + y; };
eq(R.transduce(R.map(R.inc), _add, '', [1, 2, 3, 4]), '2345');
eq(R.transduce(R.filter(isOdd), _add, '', [1, 2, 3, 4]), '13');
eq(R.transduce(R.compose(R.map(add(1)), R.take(2)), _add, '', [1, 2, 3, 4]), '23');
});
it('transduces into objects', function() {
eq(R.transduce(R.map(R.identity), R.mergeRight, {}, [{a: 1}, {b: 2, c: 3}]), {a: 1, b: 2, c: 3});
});
it('folds transformer objects over a collection with the supplied accumulator', function() {
eq(R.transduce(toxf(add), addxf, 0, [1, 2, 3, 4]), 10);
eq(R.transduce(toxf(mult), multxf, 1, [1, 2, 3, 4]), 24);
eq(R.transduce(toxf(R.concat), listxf, [0], [1, 2, 3, 4]), [0, 1, 2, 3, 4]);
eq(R.transduce(toxf(add), add, 0, [1, 2, 3, 4]), 10);
eq(R.transduce(toxf(mult), mult, 1, [1, 2, 3, 4]), 24);
});
it('dispatches to objects that implement `reduce`', function() {
var obj = {x: [1, 2, 3], reduce: function() { return 'override'; }};
eq(R.transduce(R.map(add(1)), add, 0, obj), 'override');
eq(R.transduce(R.map(add(1)), add, 10, obj), 'override');
});
it('returns the accumulator for an empty collection', function() {
eq(R.transduce(toxf(add), addxf, 0, []), 0);
eq(R.transduce(toxf(mult), multxf, 1, []), 1);
eq(R.transduce(toxf(R.concat), listxf, [], []), []);
});
it('short circuits with reduced', function() {
var transducer = R.compose(R.map(square), R.filter(isOdd));
var iterator = function(acc, val) {return val > 10 ? R.reduced(acc) : R.append(val, acc);};
var getOddSquaresWhileLessThan10 = R.transduce(transducer, iterator, []);
eq(getOddSquaresWhileLessThan10([1, 2, 3, 4]), [1, 9]);
});
});