source: node_modules/ramda/es/scan.js

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

Initial commit

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