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

first(where:)

Returns the first element of the sequence that satisfies the given predicate. 与えられた述部を満たすこのシーケンスの最初の要素を返します。

Declaration 宣言

func first(where predicate: (Element) async throws -> Bool) async rethrows -> Element?

Parameters パラメータ

predicate

A closure that takes an element of the asynchronous sequence as its argument and returns a Boolean value that indicates whether the element is a match. あるクロージャ、それは非同期シーケンスの1つの要素をそれの引数として取り、その要素があるマッチであるかどうかを指し示しているブール値を返します。

Return Value 戻り値

The first element of the sequence that satisfies predicate, or nil if there is no element that satisfies predicate. predicate満たすシーケンスの最初の要素、またはpredicateを満たす要素が無いならばnil

Discussion 解説

In this example, an asynchronous sequence called Counter produces Int values from 1 to 10. The first(where:) method returns the first member of the sequence that’s evenly divisible by both 2 and 3. この例において、Counterと呼ばれる非同期シーケンスはInt値を1から10まで生み出します。first(where:)メソッドは、23の両方で整然と割り切れるこのシーケンスの最初のメンバーを返します。


let divisibleBy2And3 = await Counter(howHigh: 10)
    .first { $0 % 2 == 0 && $0 % 3 == 0 }
print(divisibleBy2And3 ?? "none")
// Prints: 6

The predicate executes each time the asynchronous sequence produces an element, until either the predicate finds a match or the sequence ends. 述部は、非同期シーケンスがある要素を生み出すたびごとに遂行されます、述部がある合致を見つけるかシーケンスが終わるかどちらかまで。

See Also 参照

Finding Elements 要素を見つける