intersection<A> function
- Eq<
A> eq
Returns a function that computes the intersection between two ImmutableList
s.
Given two lists, list1
and list2
, it returns a new list containing
only the elements that exist in both lists based on the provided equality comparison.
Example:
final eqInt = EqInt();
final intersector = intersection(eqInt);
final list1 = ImmutableList([1, 2, 3, 4]);
final list2 = ImmutableList([3, 4, 5, 6]);
final result = intersector(list1)(list2);
print(result); // [3, 4]
Implementation
ImmutableList<A> Function(ImmutableList<A>) Function(ImmutableList<A>)
intersection<A>(Eq<A> eq) {
return (ImmutableList<A> list1) {
return (ImmutableList<A> list2) {
final itemsInList2 = list2.items.toSet();
return ImmutableList(list1.items
.where(
(item1) => itemsInList2.any((item2) => eq.equals(item1, item2)))
.toList());
};
};
}