[d24f17c] | 1 | var _curry3 =
|
---|
| 2 | /*#__PURE__*/
|
---|
| 3 | require("./internal/_curry3.js");
|
---|
| 4 | /**
|
---|
| 5 | * The `mapAccum` function behaves like a combination of map and reduce; it
|
---|
| 6 | * applies a function to each element of a list, passing an accumulating
|
---|
| 7 | * parameter from left to right, and returning a final value of this
|
---|
| 8 | * accumulator together with the new list.
|
---|
| 9 | *
|
---|
| 10 | * The iterator function receives two arguments, *acc* and *value*, and should
|
---|
| 11 | * return a tuple *[acc, value]*.
|
---|
| 12 | *
|
---|
| 13 | * @func
|
---|
| 14 | * @memberOf R
|
---|
| 15 | * @since v0.10.0
|
---|
| 16 | * @category List
|
---|
| 17 | * @sig ((acc, x) -> (acc, y)) -> acc -> [x] -> (acc, [y])
|
---|
| 18 | * @param {Function} fn The function to be called on every element of the input `list`.
|
---|
| 19 | * @param {*} acc The accumulator value.
|
---|
| 20 | * @param {Array} list The list to iterate over.
|
---|
| 21 | * @return {*} The final, accumulated value.
|
---|
| 22 | * @see R.scan, R.addIndex, R.mapAccumRight
|
---|
| 23 | * @example
|
---|
| 24 | *
|
---|
| 25 | * const digits = ['1', '2', '3', '4'];
|
---|
| 26 | * const appender = (a, b) => [a + b, a + b];
|
---|
| 27 | *
|
---|
| 28 | * R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
|
---|
| 29 | * @symb R.mapAccum(f, a, [b, c, d]) = [
|
---|
| 30 | * f(f(f(a, b)[0], c)[0], d)[0],
|
---|
| 31 | * [
|
---|
| 32 | * f(a, b)[1],
|
---|
| 33 | * f(f(a, b)[0], c)[1],
|
---|
| 34 | * f(f(f(a, b)[0], c)[0], d)[1]
|
---|
| 35 | * ]
|
---|
| 36 | * ]
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | var mapAccum =
|
---|
| 41 | /*#__PURE__*/
|
---|
| 42 | _curry3(function mapAccum(fn, acc, list) {
|
---|
| 43 | var idx = 0;
|
---|
| 44 | var len = list.length;
|
---|
| 45 | var result = [];
|
---|
| 46 | var tuple = [acc];
|
---|
| 47 |
|
---|
| 48 | while (idx < len) {
|
---|
| 49 | tuple = fn(tuple[0], list[idx]);
|
---|
| 50 | result[idx] = tuple[1];
|
---|
| 51 | idx += 1;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | return [tuple[0], result];
|
---|
| 55 | });
|
---|
| 56 |
|
---|
| 57 | module.exports = mapAccum; |
---|