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
2.0 KiB

4 years ago
  1. import _curry3 from "./internal/_curry3.js";
  2. import _reduce from "./internal/_reduce.js";
  3. /**
  4. * Returns a single item by iterating through the list, successively calling
  5. * the iterator function and passing it an accumulator value and the current
  6. * value from the array, and then passing the result to the next call.
  7. *
  8. * The iterator function receives two values: *(acc, value)*. It may use
  9. * [`R.reduced`](#reduced) to shortcut the iteration.
  10. *
  11. * The arguments' order of [`reduceRight`](#reduceRight)'s iterator function
  12. * is *(value, acc)*.
  13. *
  14. * Note: `R.reduce` does not skip deleted or unassigned indices (sparse
  15. * arrays), unlike the native `Array.prototype.reduce` method. For more details
  16. * on this behavior, see:
  17. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
  18. *
  19. * Dispatches to the `reduce` method of the third argument, if present. When
  20. * doing so, it is up to the user to handle the [`R.reduced`](#reduced)
  21. * shortcuting, as this is not implemented by `reduce`.
  22. *
  23. * @func
  24. * @memberOf R
  25. * @since v0.1.0
  26. * @category List
  27. * @sig ((a, b) -> a) -> a -> [b] -> a
  28. * @param {Function} fn The iterator function. Receives two values, the accumulator and the
  29. * current element from the array.
  30. * @param {*} acc The accumulator value.
  31. * @param {Array} list The list to iterate over.
  32. * @return {*} The final, accumulated value.
  33. * @see R.reduced, R.addIndex, R.reduceRight
  34. * @example
  35. *
  36. * R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
  37. * // - -10
  38. * // / \ / \
  39. * // - 4 -6 4
  40. * // / \ / \
  41. * // - 3 ==> -3 3
  42. * // / \ / \
  43. * // - 2 -1 2
  44. * // / \ / \
  45. * // 0 1 0 1
  46. *
  47. * @symb R.reduce(f, a, [b, c, d]) = f(f(f(a, b), c), d)
  48. */
  49. var reduce =
  50. /*#__PURE__*/
  51. _curry3(_reduce);
  52. export default reduce;