UNPKG

1.52 kBJavaScriptView Raw
1var _curryN =
2/*#__PURE__*/
3require("./internal/_curryN");
4
5var _reduce =
6/*#__PURE__*/
7require("./internal/_reduce");
8
9var _reduced =
10/*#__PURE__*/
11require("./internal/_reduced");
12/**
13 * Like [`reduce`](#reduce), `reduceWhile` returns a single item by iterating
14 * through the list, successively calling the iterator function. `reduceWhile`
15 * also takes a predicate that is evaluated before each step. If the predicate
16 * returns `false`, it "short-circuits" the iteration and returns the current
17 * value of the accumulator.
18 *
19 * @func
20 * @memberOf R
21 * @since v0.22.0
22 * @category List
23 * @sig ((a, b) -> Boolean) -> ((a, b) -> a) -> a -> [b] -> a
24 * @param {Function} pred The predicate. It is passed the accumulator and the
25 * current element.
26 * @param {Function} fn The iterator function. Receives two values, the
27 * accumulator and the current element.
28 * @param {*} a The accumulator value.
29 * @param {Array} list The list to iterate over.
30 * @return {*} The final, accumulated value.
31 * @see R.reduce, R.reduced
32 * @example
33 *
34 * const isOdd = (acc, x) => x % 2 === 1;
35 * const xs = [1, 3, 5, 60, 777, 800];
36 * R.reduceWhile(isOdd, R.add, 0, xs); //=> 9
37 *
38 * const ys = [2, 4, 6]
39 * R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
40 */
41
42
43var reduceWhile =
44/*#__PURE__*/
45_curryN(4, [], function _reduceWhile(pred, fn, a, list) {
46 return _reduce(function (acc, x) {
47 return pred(acc, x) ? fn(acc, x) : _reduced(acc);
48 }, a, list);
49});
50
51module.exports = reduceWhile;
\No newline at end of file