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

prefix(_:)

Returns an asynchronous sequence, up to the specified maximum length, containing the initial elements of the base asynchronous sequence. 指定された最大長まで、その基底非同期シーケンスの冒頭の要素を含んでいる、ある非同期シーケンスを返します。

Declaration 宣言

func prefix(_ count: Int) -> AsyncPrefixSequence<AsyncThrowingStream<Element, Failure>>

Parameters パラメータ

count

The maximum number of elements to return. The value of count must be greater than or equal to zero. 返される要素の最大限の数。countの値は、ゼロより大きいか等しくなければなりません。

Return Value 戻り値

An asynchronous sequence starting at the beginning of the base sequence with at most count elements. 基底シーケンスの先端で始まる多くともcount要素の非同期シーケンス。

Discussion 解説

Use prefix(_:) to reduce the number of elements produced by the asynchronous sequence. prefix(_:)を使って非同期シーケンスによって生み出された要素の数を減らしてください。

In this example, an asynchronous sequence called Counter produces Int values from 1 to 10. The prefix(_:) method causes the modified sequence to pass through the first six values, then end. この例において、Counterと呼ばれる非同期シーケンスはInt値を1から10まで生み出します。prefix(_:)メソッドは、その修正されたシーケンスに最初の6つの値をずっと渡させて、それから終わらせます。


for await number in Counter(howHigh: 10).prefix(6) {
    print("\(number) ")
}
// prints "1 2 3 4 5 6"

If the count passed to prefix(_:) exceeds the number of elements in the base sequence, the result contains all of the elements in the sequence. prefix(_:)に渡されるcountが基底シーケンスの中の要素の数を越えるならば、結果はそのシーケンスの要素のすべてを含みます。

See Also 参照

Selecting Elements 要素の選択