Skip to content

Commit 29c8329

Browse files
krilnonairspeedswift
authored andcommitted
[stdlib] Document that removeAll(where:) doesn't reorder the remaining elements. (swiftlang#18803)
* Document that removeAll(where:) doesn't reorder the remaining elements. Per Swift Forums discussion on the topic: "Does removeAll(where:) on arrays guarantee preserved order of elements?" * Copy docs from the first default impl up to the protocol requirement. Per Nate's feedback on PR#18803. The protocol requirement was missing a few extra sentences and a code example. * Fix a code example that would fail to remove negative odd integers. Per feedback on the forums from Jens, swap `== 1` with `!= 0` so that negative odd numbers would be removed, too, if the example were modified to include some negative numbers in the input array.
1 parent 007375b commit 29c8329

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,16 @@ public protocol RangeReplaceableCollection : Collection
343343
/// - Complexity: O(*n*), where *n* is the length of the collection.
344344
mutating func removeAll(keepingCapacity keepCapacity: Bool /*= false*/)
345345

346-
/// Removes from the collection all elements that satisfy the given predicate.
346+
/// Removes all the elements that satisfy the given predicate.
347+
///
348+
/// Use this method to remove every element in a collection that meets
349+
/// particular criteria. The order of the remaining elements is preserved.
350+
/// This example removes all the odd values from an
351+
/// array of numbers:
352+
///
353+
/// var numbers = [5, 6, 7, 8, 9, 10, 11]
354+
/// numbers.removeAll(where: { $0 % 2 != 0 })
355+
/// // numbers == [6, 8, 10]
347356
///
348357
/// - Parameter shouldBeRemoved: A closure that takes an element of the
349358
/// sequence as its argument and returns a Boolean value indicating
@@ -1083,11 +1092,12 @@ extension RangeReplaceableCollection where Self: MutableCollection {
10831092
/// Removes all the elements that satisfy the given predicate.
10841093
///
10851094
/// Use this method to remove every element in a collection that meets
1086-
/// particular criteria. This example removes all the odd values from an
1095+
/// particular criteria. The order of the remaining elements is preserved.
1096+
/// This example removes all the odd values from an
10871097
/// array of numbers:
10881098
///
10891099
/// var numbers = [5, 6, 7, 8, 9, 10, 11]
1090-
/// numbers.removeAll(where: { $0 % 2 == 1 })
1100+
/// numbers.removeAll(where: { $0 % 2 != 0 })
10911101
/// // numbers == [6, 8, 10]
10921102
///
10931103
/// - Parameter shouldBeRemoved: A closure that takes an element of the
@@ -1108,7 +1118,8 @@ extension RangeReplaceableCollection {
11081118
/// Removes all the elements that satisfy the given predicate.
11091119
///
11101120
/// Use this method to remove every element in a collection that meets
1111-
/// particular criteria. This example removes all the vowels from a string:
1121+
/// particular criteria. The order of the remaining elements is preserved.
1122+
/// This example removes all the vowels from a string:
11121123
///
11131124
/// var phrase = "The rain in Spain stays mainly in the plain."
11141125
///

0 commit comments

Comments
 (0)