xprod.js 995 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import _curry2 from "./internal/_curry2.js";
  2. /**
  3. * Creates a new list out of the two supplied by creating each possible pair
  4. * from the lists.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.1.0
  9. * @category List
  10. * @sig [a] -> [b] -> [[a,b]]
  11. * @param {Array} as The first list.
  12. * @param {Array} bs The second list.
  13. * @return {Array} The list made by combining each possible pair from
  14. * `as` and `bs` into pairs (`[a, b]`).
  15. * @example
  16. *
  17. * R.xprod([1, 2], ['a', 'b']); //=> [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
  18. * @symb R.xprod([a, b], [c, d]) = [[a, c], [a, d], [b, c], [b, d]]
  19. */
  20. var xprod =
  21. /*#__PURE__*/
  22. _curry2(function xprod(a, b) {
  23. // = xprodWith(prepend); (takes about 3 times as long...)
  24. var idx = 0;
  25. var ilen = a.length;
  26. var j;
  27. var jlen = b.length;
  28. var result = [];
  29. while (idx < ilen) {
  30. j = 0;
  31. while (j < jlen) {
  32. result[result.length] = [a[idx], b[j]];
  33. j += 1;
  34. }
  35. idx += 1;
  36. }
  37. return result;
  38. });
  39. export default xprod;