source: node_modules/ramda/src/scan.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3.js");
4
5var _dispatchable =
6/*#__PURE__*/
7require("./internal/_dispatchable.js");
8
9var _xscan =
10/*#__PURE__*/
11require("./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
37var 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
55module.exports = scan;
Note: See TracBrowser for help on using the repository browser.