forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.js
42 lines (34 loc) · 1.44 KB
/
scan.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
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var sinon = require('sinon');
describe('scan', function() {
var add = function(a, b) {return a + b;};
var mult = function(a, b) {return a * b;};
var double = function(x) {return 2 * x;};
it('scans simple functions over arrays with the supplied accumulator', function() {
eq(R.scan(add, 0, [1, 2, 3, 4]), [0, 1, 3, 6, 10]);
eq(R.scan(mult, 1, [1, 2, 3, 4]), [1, 1, 2, 6, 24]);
});
it('returns the accumulator for an empty array', function() {
eq(R.scan(add, 0, []), [0]);
eq(R.scan(mult, 1, []), [1]);
});
it('works with transducers', function() {
eq(R.into([], R.scan(add, 0), [1, 2, 3, 4]), [0, 1, 3, 6, 10]);
eq(R.into([], R.scan(mult, 1), []), [1]);
});
it('composes with other transducers', function() {
eq(R.into([], R.compose(R.scan(mult, 1), R.take(2)), [1, 2, 3, 4]), [1, 1]);
eq(R.into([], R.compose(R.scan(mult, 1), R.map(double)), [1, 2, 3, 4]), [2, 2, 4, 12, 48]);
});
it('works lazily: taking 3 elements must call reducer twice', function() {
var reducer = sinon.spy();
R.into([], R.compose(R.scan(reducer, 0), R.take(3)), [1, 2, 3, 4]);
sinon.assert.calledTwice(reducer);
});
it('works lazily: taking 0 elements must call reducer 0 times', function() {
var reducer = sinon.spy();
R.into([], R.compose(R.scan(reducer, 0), R.take(0)), [1, 2, 3, 4]);
sinon.assert.notCalled(reducer);
});
});