You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

4 years ago
  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. var forEach =
  38. /*#__PURE__*/
  39. _curry2(
  40. /*#__PURE__*/
  41. _checkForMethod('forEach', function forEach(fn, list) {
  42. var len = list.length;
  43. var idx = 0;
  44. while (idx < len) {
  45. fn(list[idx]);
  46. idx += 1;
  47. }
  48. return list;
  49. }));
  50. export default forEach;