1 | var _curryN =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_curryN.js");
|
---|
4 |
|
---|
5 | var _xReduce =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_xReduce.js");
|
---|
8 |
|
---|
9 | var _xwrap =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./internal/_xwrap.js");
|
---|
12 |
|
---|
13 | var _reduced =
|
---|
14 | /*#__PURE__*/
|
---|
15 | require("./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 |
|
---|
48 | var 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 |
|
---|
58 | module.exports = reduceWhile; |
---|