subtract method

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

Removes elements one at a time if they are present.

1, 2, 3, 1.subtract(1) returns 2, 3, 1.

1, 2, 3, 1.subtract(1, 1) returns 2, 3.

[{1, 2}, {1, 2}, 3, 4].subtract({1, 2}) returns [{1, 2}, 3, 4].

To determine which elements to subtract, h.symmetricDeepEquals uses DeepCollectionEquality().equals in both directions since normally it is asymmetrical. If using a custom equalityFunction, elements are compared with the element in the original being the first argument, and element elementsToSubtract being the second.

subtractAll can remove all occurrences of each element.

Implementation

Set<E> subtract(
  Iterable<E> elementsToSubtract, {
  bool Function(E a, E b) equalityFunction = h.symmetricDeepEquals,
}) {
  return h
      .subtractIterable(
        original: this,
        elementsToRemove: elementsToSubtract,
        equalityFunction: equalityFunction,
      )
      .toSet();
}