Instance Method インスタンスメソッド

prefix(while:)

Returns an asynchronous sequence, containing the initial, consecutive elements of the base sequence that satisfy the given predicate. ある非同期シーケンスを返します、基底シーケンスの冒頭の、連続する要素いくつかを含んでいます、それらは与えられた述部を満たすものです。

Declaration 宣言

func prefix(while predicate: @escaping (Element) async -> Bool) rethrows -> AsyncPrefixWhileSequence<AsyncThrowingStream<Element, Failure>>

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. 冒頭の、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. prefix(while:)を使うことで、基底シーケンスからの要素があなたが指定する条件に合う間ずっと値を生み出してください。修正されたシーケンスは、条件クロージャが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: この例において、Counterと呼ばれる非同期シーケンスはInt値を1から10まで生み出します。prefix(while:)メソッドは、修正されたシーケンスが値それらを、それらが23によって割り切れない限り通過させるようにします。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"

See Also 参照

Selecting Elements 要素の選択