source: node_modules/ramda/src/reduceWhile.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.7 KB
Line 
1var _curryN =
2/*#__PURE__*/
3require("./internal/_curryN.js");
4
5var _xReduce =
6/*#__PURE__*/
7require("./internal/_xReduce.js");
8
9var _xwrap =
10/*#__PURE__*/
11require("./internal/_xwrap.js");
12
13var _reduced =
14/*#__PURE__*/
15require("./internal/_reduced.js");
16/**
17 * Like [`reduce`](#reduce), `reduceWhile` returns a single item by iterating
18 * through the list, successively calling the iterator function. `reduceWhile`
19 * also takes a predicate that is evaluated before each step. If the predicate
20 * returns `false`, it "short-circuits" the iteration and returns the current
21 * value of the accumulator. `reduceWhile` may alternatively be short-circuited
22 * via [`reduced`](#reduced).
23 *
24 * @func
25 * @memberOf R
26 * @since v0.22.0
27 * @category List
28 * @sig ((a, b) -> Boolean) -> ((a, b) -> a) -> a -> [b] -> a
29 * @param {Function} pred The predicate. It is passed the accumulator and the
30 * current element.
31 * @param {Function} fn The iterator function. Receives two values, the
32 * accumulator and the current element.
33 * @param {*} a The accumulator value.
34 * @param {Array} list The list to iterate over.
35 * @return {*} The final, accumulated value.
36 * @see R.reduce, R.reduced
37 * @example
38 *
39 * const isOdd = (acc, x) => x % 2 !== 0;
40 * const xs = [1, 3, 5, 60, 777, 800];
41 * R.reduceWhile(isOdd, R.add, 0, xs); //=> 9
42 *
43 * const ys = [2, 4, 6]
44 * R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
45 */
46
47
48var reduceWhile =
49/*#__PURE__*/
50_curryN(4, [], function _reduceWhile(pred, fn, a, list) {
51 var xf = _xwrap(function (acc, x) {
52 return pred(acc, x) ? fn(acc, x) : _reduced(acc);
53 });
54
55 return _xReduce(xf, a, list);
56});
57
58module.exports = reduceWhile;
Note: See TracBrowser for help on using the repository browser.