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.

39 lines
1022 B

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