UNPKG

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