keep method

Set<E> keep(
  1. Iterable<E> elementsToKeep, {
  2. bool equalityFunction(
    1. E a,
    2. E b
    ) = h.symmetricDeepEquals,
})

Keeps all elements that are also in elementsToKeep.

1, 1, 2, 2.keep(1, 3, 5) returns 1, 1.

Keeps duplicates in original value. Duplicates in elementsToKeep have no effect. Can use nub to remove duplicates.

Default h.symmetricDeepEquals uses DeepCollectionEquality().equals in both directions since it is normally asymmetric. Custom equalityFunction can be used to determine if elements are already present. Elements in elementsToKeep will be the second argument for asymmetric equality functions.

Implementation

Set<E> keep(
  Iterable<E> elementsToKeep, {
  bool Function(E a, E b) equalityFunction = h.symmetricDeepEquals,
}) {
  return h
      .keepElements(
        original: this,
        input: elementsToKeep,
        equalityFunction: equalityFunction,
      )
      .toSet();
}