1
- # Adding in-place ` remove (where:)` to the Standard Library
1
+ # Adding in-place ` removeAll (where:)` to the Standard Library
2
2
3
3
* Proposal: [ SE-0197] ( 0197-remove-where.md )
4
4
* Author: [ Ben Cohen] ( https://github.com/airspeedswift )
5
5
* Review Manager: [ John McCall] ( https://github.com/rjmccall )
6
- * Status: ** Active review (January 25...30) **
6
+ * Status: ** Accepted with Revisions **
7
7
* Implementation: [ apple/swift #11576 ] ( https://github.com/apple/swift/pull/11576 )
8
+ * Review: [ Thread] ( https://forums.swift.org/t/se-0197-add-in-place-remove-where/8872 )
9
+ * Previous Revision: [ 1] ( https://github.com/apple/swift-evolution/blob/feec7890d6c193e9260ac9905456f25ef5656acd/proposals/0197-remove-where.md )
8
10
9
11
## Introduction
10
12
11
13
It is common to want to remove all occurrences of a certain element from a
12
- collection. This proposal is to add a ` remove ` algorithm to the
14
+ collection. This proposal is to add a ` removeAll ` algorithm to the
13
15
standard library, which will remove all entries in a collection in-place
14
16
matching a given predicate.
15
17
@@ -65,7 +67,7 @@ important example of this, because its elements (graphemes) are variable width.
65
67
Add the following method to ` RangeReplaceableCollection ` :
66
68
67
69
``` swift
68
- nums.remove (where : isOdd)
70
+ nums.removeAll (where : isOdd)
69
71
```
70
72
71
73
The default implementation will use the protocol's ` init() ` and ` append(_:) `
@@ -90,11 +92,11 @@ Add the following to `RangeReplaceableCollection`:
90
92
``` swift
91
93
protocol RangeReplaceableCollection {
92
94
/// Removes every element satisfying the given predicate from the collection.
93
- mutating func remove (where : (Iterator .Element ) throws -> Bool ) rethrows
95
+ mutating func removeAll (where : (Iterator .Element ) throws -> Bool ) rethrows
94
96
}
95
97
96
98
extension RangeReplaceableCollection {
97
- mutating func remove (where : (Iterator .Element ) throws -> Bool ) rethrows {
99
+ mutating func removeAll (where : (Iterator .Element ) throws -> Bool ) rethrows {
98
100
// default implementation similar to self = self.filter
99
101
}
100
102
}
@@ -119,7 +121,7 @@ This change is purely additive so has no API resilience consequences.
119
121
120
122
## Alternatives considered
121
123
122
- ` remove (where:)` takes a closure with ` true ` for elements to remove.
124
+ ` removeAll (where:)` takes a closure with ` true ` for elements to remove.
123
125
` filter ` takes a closure with elements to keep. In both cases, ` true ` is the
124
126
"active" case, so likely to be what the user wants without having to apply a
125
127
negation. The naming of ` filter ` is unfortunately ambiguous as to whether it's
@@ -131,3 +133,8 @@ have an equivalent for collections of `Equatable` elements. A similar addition
131
133
could be made that removes every element equal to a given value. This could
132
134
easily be done as a further additive proposal later.
133
135
136
+ The initial proposal of this feature named it ` remove(where:) ` . During review,
137
+ it was agreed that this was unnecessarily ambiguous about whether all the
138
+ matching elements should be removed or just the first, and so the method was
139
+ renamed to ` removeAll(where:) ` .
140
+
0 commit comments