1 | import _checkForMethod from "./internal/_checkForMethod.js";
|
---|
2 | import _curry2 from "./internal/_curry2.js";
|
---|
3 | /**
|
---|
4 | * Iterate over an input `list`, calling a provided function `fn` for each
|
---|
5 | * element in the list.
|
---|
6 | *
|
---|
7 | * `fn` receives one argument: *(value)*.
|
---|
8 | *
|
---|
9 | * Note: `R.forEach` does not skip deleted or unassigned indices (sparse
|
---|
10 | * arrays), unlike the native `Array.prototype.forEach` method. For more
|
---|
11 | * details on this behavior, see:
|
---|
12 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
|
---|
13 | *
|
---|
14 | * Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns
|
---|
15 | * the original array. In some libraries this function is named `each`.
|
---|
16 | *
|
---|
17 | * Dispatches to the `forEach` method of the second argument, if present.
|
---|
18 | *
|
---|
19 | * @func
|
---|
20 | * @memberOf R
|
---|
21 | * @since v0.1.1
|
---|
22 | * @category List
|
---|
23 | * @sig (a -> *) -> [a] -> [a]
|
---|
24 | * @param {Function} fn The function to invoke. Receives one argument, `value`.
|
---|
25 | * @param {Array} list The list to iterate over.
|
---|
26 | * @return {Array} The original list.
|
---|
27 | * @see R.addIndex
|
---|
28 | * @example
|
---|
29 | *
|
---|
30 | * const printXPlusFive = x => console.log(x + 5);
|
---|
31 | * R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
|
---|
32 | * // logs 6
|
---|
33 | * // logs 7
|
---|
34 | * // logs 8
|
---|
35 | * @symb R.forEach(f, [a, b, c]) = [a, b, c]
|
---|
36 | */
|
---|
37 |
|
---|
38 | var forEach =
|
---|
39 | /*#__PURE__*/
|
---|
40 | _curry2(
|
---|
41 | /*#__PURE__*/
|
---|
42 | _checkForMethod('forEach', function forEach(fn, list) {
|
---|
43 | var len = list.length;
|
---|
44 | var idx = 0;
|
---|
45 |
|
---|
46 | while (idx < len) {
|
---|
47 | fn(list[idx]);
|
---|
48 | idx += 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | return list;
|
---|
52 | }));
|
---|
53 |
|
---|
54 | export default forEach; |
---|