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