實例方法
prefix(while:)
返回一個異步序列,包含滿足給定謂詞的基本序列的初始連續元素。
聲明
@preconcurrency func prefix(while predicate: @escaping (Self.Element) async -> Bool) rethrows -> AsyncPrefixWhileSequence<Self>
返回值
滿足 predicate
的初始連續元素的異步序列。
參數
predicate
一個閉包,它將元素作為參數並返回一個布爾值,指示該元素是否應包含在修改後的序列中。
詳述
當基本序列中的元素滿足您指定的條件時,使用prefix(while:)
生成值。當謂詞閉包返回 false
時,修改後的序列結束。
在此示例中,名為 Counter
的異步序列生成從 1
到 10
的 Int
值。 prefix(while:)
方法會導致修改後的序列傳遞值,隻要它們不能被 2
和 3
整除。到達 6
後,序列結束:
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"
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift AsyncMapSequence prefix(_:)用法及代碼示例
- Swift AsyncMapSequence max()用法及代碼示例
- Swift AsyncMapSequence min()用法及代碼示例
- Swift AsyncMapSequence compactMap(_:)用法及代碼示例
- Swift AsyncMapSequence contains(_:)用法及代碼示例
- Swift AsyncMapSequence max(by:)用法及代碼示例
- Swift AsyncMapSequence contains(where:)用法及代碼示例
- Swift AsyncMapSequence map(_:)用法及代碼示例
- Swift AsyncMapSequence first(where:)用法及代碼示例
- Swift AsyncMapSequence drop(while:)用法及代碼示例
- Swift AsyncMapSequence min(by:)用法及代碼示例
- Swift AsyncMapSequence flatMap(_:)用法及代碼示例
- Swift AsyncMapSequence allSatisfy(_:)用法及代碼示例
- Swift AsyncMapSequence reduce(_:_:)用法及代碼示例
- Swift AsyncMapSequence filter(_:)用法及代碼示例
- Swift AsyncMapSequence dropFirst(_:)用法及代碼示例
- Swift AsyncThrowingFlatMapSequence min()用法及代碼示例
- Swift AsyncPrefixWhileSequence reduce(_:_:)用法及代碼示例
- Swift AsyncThrowingMapSequence reduce(_:_:)用法及代碼示例
- Swift AsyncDropWhileSequence allSatisfy(_:)用法及代碼示例
- Swift AsyncSequence dropFirst(_:)用法及代碼示例
- Swift AsyncDropFirstSequence filter(_:)用法及代碼示例
- Swift AsyncSequence reduce(_:_:)用法及代碼示例
- Swift AsyncFlatMapSequence min()用法及代碼示例
- Swift AsyncThrowingPrefixWhileSequence drop(while:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 AsyncMapSequence prefix(while:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。