zipWith.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import _curry3 from "./internal/_curry3.js";
  2. /**
  3. * Creates a new list out of the two supplied by applying the function to each
  4. * equally-positioned pair in the lists. The returned list is truncated to the
  5. * length of the shorter of the two input lists.
  6. *
  7. * @function
  8. * @memberOf R
  9. * @since v0.1.0
  10. * @category List
  11. * @sig ((a, b) -> c) -> [a] -> [b] -> [c]
  12. * @param {Function} fn The function used to combine the two elements into one value.
  13. * @param {Array} list1 The first array to consider.
  14. * @param {Array} list2 The second array to consider.
  15. * @return {Array} The list made by combining same-indexed elements of `list1` and `list2`
  16. * using `fn`.
  17. * @example
  18. *
  19. * const f = (x, y) => {
  20. * // ...
  21. * };
  22. * R.zipWith(f, [1, 2, 3], ['a', 'b', 'c']);
  23. * //=> [f(1, 'a'), f(2, 'b'), f(3, 'c')]
  24. * @symb R.zipWith(fn, [a, b, c], [d, e, f]) = [fn(a, d), fn(b, e), fn(c, f)]
  25. */
  26. var zipWith =
  27. /*#__PURE__*/
  28. _curry3(function zipWith(fn, a, b) {
  29. var rv = [];
  30. var idx = 0;
  31. var len = Math.min(a.length, b.length);
  32. while (idx < len) {
  33. rv[idx] = fn(a[idx], b[idx]);
  34. idx += 1;
  35. }
  36. return rv;
  37. });
  38. export default zipWith;