# SE-0197 — Add in-place remove(where:)

**URL:** <https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872>\
**Category:** Proposal Reviews\
**Created:** [January 25, 2018, 6:12pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872 "2018-01-25T18:12:55Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![hooman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hooman/32/43536_2.png) [@hooman](https://forums.swift.org/u/hooman)\
**Post date:** [January 26, 2018, 11:18pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/21 "2018-01-26T23:18:44Z")

</div>

+1 for `removeAll(where:)` for the reasons noted by Nate (@nnnnnnnn) and others.

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [January 27, 2018, 1:12am UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/22 "2018-01-27T01:12:59Z")

</div>

I'm with @Paul_Cantrell. I'd prefer to add four new keywords that follow the Swift API guidelines than try to fight an industry term of art to wrestle the right behavior into the language by pairing `filter` with `removeAll`. (Thanks @nnnnnnnn for the improved naming btw.)

This isn't quite a "if you were designing it from scratch" situation but it's close. And in such case, the utility of positive and negative logic versions is evident. I'd want to `filter` a sequence but maybe I'd prefer to `selectAll` , `selectingAll`, `removeAll`, `removingAll` an array, set, or dictionary.

---

<div class="post-metadata">

**Author:** ![MichaelH](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michaelh/32/707_2.png) [@MichaelH](https://forums.swift.org/u/MichaelH)\
**Post date:** [January 27, 2018, 10:21am UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/23 "2018-01-27T10:21:54Z")

</div>

The proposal mentions concrete implementations for Set and Dictionary, but those do not seem to be part of the implementation. Are those coming as well? This seems especially useful for Dictionary:

dict.remove[All] { isOdd($0.value) }

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [January 27, 2018, 11:13am UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/24 "2018-01-27T11:13:32Z")

</div>

Good point. I'll add them to the PR.

---

<div class="post-metadata">

**Author:** ![aidantwoods](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aidantwoods/32/1927_2.png) [@aidantwoods](https://forums.swift.org/u/aidantwoods)\
**Post date:** [January 29, 2018, 10:32am UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/25 "2018-01-29T10:32:19Z")

</div>

> What is your evaluation of the proposal?

+1

The following point is a bit orthogonal to the performance aspect, or improving the readability of `filter` meaning "keep", but I wanted to point out that if all you're looking to do is change a "keep" filter into a "remove" one then the readability of `nums = nums.filter { !isOdd($0) }` could be slightly improved by extending the `!` operator to act on `Bool` returning functions, e.g.

```swift
prefix func !<D>(_ fn: @escaping (D) -> Bool) -> (D) -> Bool { return { !fn($0) } }

```

Then the example reduces to:

```swift
var nums = [1,2,3,4,5]
// remove odd elements
nums = nums.filter(!isOdd)

```

In-fact this may be useful for avoiding writing a closure when using this new in-place `remove(where:)` when simply the negation of a function is desired.

To remove even numbers, you could write `nums.remove(where: !isOdd)` for example.

That said, I think there is enough value in the performance aspect, and the convenience of being an in-place removal if dealing with an already mutable collection to warrant adding this new method.

> Is the problem being addressed significant enough to warrant a change to Swift?

Yes.

> Does this proposal fit well with the feel and direction of Swift?

Yes.

> How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

A couple of quick reads though.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [January 30, 2018, 10:47pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/26 "2018-01-30T22:47:21Z")

</div>

Any more comments before review concludes? Currently it seems like consensus is in favor in adding the operation, but spelled `removeAll(where:)`.

---

<div class="post-metadata">

**Author:** ![MichaelH](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michaelh/32/707_2.png) [@MichaelH](https://forums.swift.org/u/MichaelH)\
**Post date:** [January 31, 2018, 6:40am UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/27 "2018-01-31T06:40:08Z")

</div>

+1 on the proposal and I also support the spelling `removeAll(where:)`, as the method is basically a constrained version of `removeAll()`.

Once [SE-0174](https://github.com/apple/swift-evolution/blob/master/proposals/0174-filter-range-replaceable.md) lands, could this method also be added to `Sequence where Filtered == Self`? That should cover Dictionary and Set and be useful for some user-defined types as well.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [February 2, 2018, 4:06am UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/28 "2018-02-02T04:06:59Z")

</div>

The review of this proposal is now over. [The core team has decided](https://forums.swift.org/t/accepted-with-revision-se-0197-add-in-place-remove-where/9459) to **accept** the proposal with the revision that the method be named `removeAll(where:)`.

In this thread, we also discussed whether to add a `removeAll(_: Element)` method. While I think that's a reasonable-sounding idea, I do think it can considered independently of SE-0197, since it wasn't originally part of the pitch. If you're interested in adding that method, I encourage you to start a new pitch thread for it.

---

<div class="post-metadata">

**Author:** ![krzyzanowskim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krzyzanowskim/32/1282_2.png) [@krzyzanowskim](https://forums.swift.org/u/krzyzanowskim)\
**Post date:** [April 20, 2018, 1:55pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/29 "2018-04-20T13:55:32Z")

</div>

I'm a bit late for this one, Just want to point out that we now have:

**drop(while:)**  
and the new  
**removeAll(where:)**

both are somehow related and I'd love it we'll eventually, at some point, end up with **drop(where:)** too, or **removeAll(while:)**

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [April 21, 2018, 8:16pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/30 "2018-04-21T20:16:30Z")

</div>

`Sequence.drop(while:)` walks the elements in order and stops at the first disqualifying one, while `Collection.removeAll(where:)` conceptually checks all elements and ignores their iteration order. So, giving each method an overload for the other one's mode wouldn't make sense.

---

<div class="post-metadata">

**Author:** ![Tino](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tino/32/1073_2.png) [@Tino](https://forums.swift.org/u/Tino)\
**Post date:** [April 25, 2018, 3:22pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/31 "2018-04-25T15:22:00Z")

</div>

I think @krzyzanowskim just wanted to say that we have two different verbs for methods that get rid of elements -- and probably was fooled by one name, which isn't `dropping(while:)` as you might expect because of our rules.  
I think both names make sense in isolation, but it seems arbitrary that one operation exists only in a form that creates a new object, and the other only has a mutating variant.

---

<div class="post-metadata">

**Author:** ![krzyzanowskim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krzyzanowskim/32/1282_2.png) [@krzyzanowskim](https://forums.swift.org/u/krzyzanowskim)\
**Post date:** [May 11, 2018, 8:28pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/32 "2018-05-11T20:28:29Z")

</div>

thanks, that was exactly my point. thanks!

I understand that English grammar is important, but sometimes I feel like Swift is one step too far with the naming ;)

---

<div class="post-metadata">

**Author:** ![Kentzo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kentzo/32/7478_2.png) [@Kentzo](https://forums.swift.org/u/Kentzo)\
**Post date:** [November 10, 2020, 5:01pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/33 "2020-11-10T17:01:58Z")

</div>

I hate to bump an old thread, but seems like `removeAll(where:)` is missing in both Dictionary and Set as of 5.3. Cannot find the discussion of why their implementations were dropped.

---

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [November 10, 2020, 5:44pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/34 "2020-11-10T17:44:05Z")

</div>

Were they ever on Dictionary and Set? The proposal talks about `RangeReplaceableCollection` which those two are not.

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [November 10, 2020, 5:47pm UTC](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872/35 "2020-11-10T17:47:44Z")

</div>

It does look like they were in the proposal but omitted from the implementation:

> Since `Dictionary` and `Set` would benefit from this functionality as well, but are not range-replaceable, they should be given concrete implementations for consistency.

We recently adopted rules that such missing parts of implementations would expire after a year. I don't recall if there was a stumbling block in implementing these or if it was just missed. I'll check and if it's a simple omission, should be an easy re-propose.

[Previous page](https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872.md?page=1)
