1 | var _curry3 =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_curry3.js");
|
---|
4 |
|
---|
5 | var _dispatchable =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_dispatchable.js");
|
---|
8 |
|
---|
9 | var _xscan =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./internal/_xscan.js");
|
---|
12 | /**
|
---|
13 | * Scan is similar to [`reduce`](#reduce), but returns a list of successively
|
---|
14 | * reduced values from the left.
|
---|
15 | *
|
---|
16 | * Acts as a transducer if a transformer is given in list position.
|
---|
17 | *
|
---|
18 | * @func
|
---|
19 | * @memberOf R
|
---|
20 | * @since v0.10.0
|
---|
21 | * @category List
|
---|
22 | * @sig ((a, b) -> a) -> a -> [b] -> [a]
|
---|
23 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
---|
24 | * current element from the array
|
---|
25 | * @param {*} acc The accumulator value.
|
---|
26 | * @param {Array} list The list to iterate over.
|
---|
27 | * @return {Array} A list of all intermediately reduced values.
|
---|
28 | * @see R.reduce, R.mapAccum
|
---|
29 | * @example
|
---|
30 | *
|
---|
31 | * const numbers = [1, 2, 3, 4];
|
---|
32 | * const factorials = R.scan(R.multiply, 1, numbers); //=> [1, 1, 2, 6, 24]
|
---|
33 | * @symb R.scan(f, a, [b, c]) = [a, f(a, b), f(f(a, b), c)]
|
---|
34 | */
|
---|
35 |
|
---|
36 |
|
---|
37 | var scan =
|
---|
38 | /*#__PURE__*/
|
---|
39 | _curry3(
|
---|
40 | /*#__PURE__*/
|
---|
41 | _dispatchable([], _xscan, function scan(fn, acc, list) {
|
---|
42 | var idx = 0;
|
---|
43 | var len = list.length;
|
---|
44 | var result = [acc];
|
---|
45 |
|
---|
46 | while (idx < len) {
|
---|
47 | acc = fn(acc, list[idx]);
|
---|
48 | result[idx + 1] = acc;
|
---|
49 | idx += 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | return result;
|
---|
53 | }));
|
---|
54 |
|
---|
55 | module.exports = scan; |
---|