Skip to content

Commit 8014849

Browse files
committed
Document partitioned(_:) on Collection
1 parent 1b24cac commit 8014849

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

Sources/Algorithms/Partition.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,35 @@ extension Sequence {
264264
}
265265

266266
extension Collection {
267-
// This is a specialized version of the same algorithm on `Sequence` that
268-
// avoids reallocation of arrays since `count` is known ahead of time.
267+
/// Returns two arrays containing, in order, the elements of the collection
268+
/// that do and don’t satisfy the given predicate, respectively.
269+
///
270+
/// In this example, `partitioned(_:)` is used to separate the input based on
271+
/// names that aren’t and are shorter than five characters, respectively:
272+
///
273+
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"]
274+
/// let (longNames, shortNames) = cast.partitioned({ $0.count < 5 })
275+
/// print(longNames)
276+
/// // Prints "["Vivien", "Marlon"]"
277+
/// print(shortNames)
278+
/// // Prints "["Kim", "Karl"]"
279+
///
280+
/// - Parameter belongsInSecondCollection: A closure that takes an element of
281+
/// the collection as its argument and returns a Boolean value indicating
282+
/// whether the element should be included in the second returned array.
283+
/// Otherwise, the element will appear in the first returned array.
284+
///
285+
/// - Returns: Two arrays with with all of the elements of the receiver. The
286+
/// first array contains all the elements that `belongsInSecondCollection`
287+
/// didn’t allow, and the second array contains all the elements that
288+
/// `belongsInSecondCollection` allowed.
289+
///
290+
/// - Complexity: O(*n*), where *n* is the length of the collection.
291+
///
292+
/// - Note: This function on `Collection` performs a bit faster than the
293+
/// `Sequence` implementation. Since the size of the collection, and therefore
294+
/// the total size of the two returned arrays, is known ahead of time, it can
295+
/// avoid array resizing.
269296
@inlinable
270297
public func partitioned(
271298
_ belongsInSecondCollection: (Element) throws -> Bool

0 commit comments

Comments
 (0)