partitioned(by:)

Returns two arrays containing the elements of the collection that don’t and do satisfy the given predicate, respectively.

Partition.swift:284
func partitioned(by predicate: (Element) throws -> Bool) rethrows -> (falseElements: [Element], trueElements: [Element])

Parameters

predicate

A closure that takes an element of the collection as its argument and returns a Boolean value indicating whether the element should be included in the second returned array. Otherwise, the element will appear in the first returned array.

Returns

Two arrays with all of the elements of the receiver. The first array contains all the elements that predicate didn’t allow, and the second array contains all the elements that predicate allowed. The order of the elements in the arrays matches the order of the elements in the original collection.

In this example, partitioned(by:) is used to separate the input based on whether a name is shorter than five characters.

let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let (longNames, shortNames) = cast.partitioned(by: { $0.count < 5 })
print(longNames)
// Prints "["Vivien", "Marlon"]"
print(shortNames)
// Prints "["Kim", "Karl"]"