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

prefix(while:)

Returns a sequence containing the initial, consecutive elements that satisfy the given predicate. 与えられた述部を満たす、冒頭の、隣接する要素を含んでいるあるシーケンスを返します。

Declaration 宣言

func prefix(while predicate: (Base.Element) throws -> Bool) rethrows -> [Base.Element]

Parameters パラメータ

predicate

A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the result. あるクロージャ、それはシーケンスの1つの要素をそれの引数として取り、その要素が結果に含まれるべきかどうかを指し示しているブール値を返します。

Return Value 戻り値

A sequence of the initial, consecutive elements that satisfy predicate. predicateを満たす冒頭の、隣接する要素からなるあるシーケンス。

Discussion 解説

The following example uses the prefix(while:) method to find the positive numbers at the beginning of the numbers array. Every element of numbers up to, but not including, the first negative value is included in the result. 以下の例は、prefix(while:)メソッドを使って、正の数をnumbers配列の始まりで見つけます。最初の負の値までの、しかしそれを含まない、numbersのすべての要素は、この結果の中に含まれます。


let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let positivePrefix = numbers.prefix(while: { $0 > 0 })
// positivePrefix == [3, 7, 4]

If predicate matches every element in the sequence, the resulting sequence contains every element of the sequence. predicateがシーケンスの中のすべての要素に合致するならば、結果のシーケンスはそのシーケンスのすべての要素を含みます。

Complexity: O(k), where k is the length of the result. 計算量:O(k)、ここでkは結果の長さです。