[Pitch] Add `insertOrRemove(_:)` Method to `Set` and `SetAlgebra` in Swift

I also implement this as SetAlgebra.toggle(_:), but I go a bit further: It returns a discardable Bool to indicate whether the set contains the element or not after toggling it. This matches the general pattern of insert(_:), update(with:), and remove(_:). It also makes it a bit more convenient to perform logic based on the operation, such as:

if mySet.toggle(someValue) {
    print("The value is now present in the set")
} else {
    print("The value is no longer present in the set")
}

(and FWIW I vastly prefer the naming of toggle(_:), since it matches Bool.toggle(), which is conceptually similar)

25 Likes