prefix(while:)
Returns an asynchronous sequence, containing the initial, consecutive elements of the base sequence that satisfy the given predicate.
Declaration
@preconcurrency func prefix(while predicate: @escaping @Sendable (Self.Element) async -> Bool) rethrows -> AsyncPrefixWhileSequence<Self>Parameters
- predicate:
A closure that takes an element as a parameter and returns a Boolean value indicating whether the element should be included in the modified sequence.
Return Value
An asynchronous sequence of the initial, consecutive elements that satisfy predicate.
Discussion
Use prefix(while:) to produce values while elements from the base sequence meet a condition you specify. The modified sequence ends when the predicate closure returns false.
In this example, an asynchronous sequence called Counter produces Int values from 1 to 10. The prefix(while:) method causes the modified sequence to pass along values so long as they aren’t divisible by 2 and 3. Upon reaching 6, the sequence ends:
let stream = Counter(howHigh: 10)
.prefix { $0 % 2 != 0 || $0 % 3 != 0 }
for try await number in stream {
print(number, terminator: " ")
}
// Prints "1 2 3 4 5 "