intersection.js 890 B

1234567891011121314151617181920212223242526272829303132333435
  1. import _curry2 from "./internal/_curry2.js";
  2. import _filter from "./internal/_filter.js";
  3. import _Set from "./internal/_Set.js";
  4. import uniq from "./uniq.js";
  5. /**
  6. * Combines two lists into a set (i.e. no duplicates) composed of those
  7. * elements common to both lists.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.1.0
  12. * @category Relation
  13. * @sig [*] -> [*] -> [*]
  14. * @param {Array} list1 The first list.
  15. * @param {Array} list2 The second list.
  16. * @return {Array} The list of elements found in both `list1` and `list2`.
  17. * @see R.innerJoin
  18. * @example
  19. *
  20. * R.intersection([1,2,3,4], [7,6,5,4,3]); //=> [4, 3]
  21. */
  22. var intersection =
  23. /*#__PURE__*/
  24. _curry2(function intersection(list1, list2) {
  25. var toKeep = new _Set();
  26. for (var i = 0; i < list1.length; i += 1) {
  27. toKeep.add(list1[i]);
  28. }
  29. return uniq(_filter(toKeep.has.bind(toKeep), list2));
  30. });
  31. export default intersection;