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

prefix(_:)

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

Declaration 宣言

func prefix(_ maxLength: Int) -> PrefixSequence<LazyMapSequence<Base, Element>.Iterator>

Parameters パラメータ

maxLength

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

Return Value 戻り値

A sequence starting at the beginning of this sequence with at most maxLength elements. このシーケンスの先端で始まる多くともmaxLength要素のあるシーケンス。

Discussion 解説

If the maximum length exceeds the number of elements in the sequence, the result contains all the elements in the sequence. 最大長がシーケンスの要素の数を越えるならば、結果はシーケンスの要素すべてを含みます。


let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints "[1, 2]"
print(numbers.prefix(10))
// Prints "[1, 2, 3, 4, 5]"

Complexity: O(1) 計算量:O(1)