Instance Method

prefix(through:)

Returns a subsequence from the start of the collection through the specified position.

Declaration

func prefix(through position: Int) -> Slice<MLDataTable.Rows>

Parameters

end

The index of the last element to include in the resulting subsequence. end must be a valid index of the collection that is not equal to the endIndex property.

Return Value

A subsequence up to, and including, the end position.

Discussion

The resulting subsequence includes the element at the position end. The following example searches for the index of the number 40 in an array of integers, and then prints the prefix of the array up to, and including, that index:

let numbers = [10, 20, 30, 40, 50, 60]
if let i = numbers.firstIndex(of: 40) {
    print(numbers.prefix(through: i))
}
// Prints "[10, 20, 30, 40]"

Using the prefix(through:) method is equivalent to using a partial closed range as the collection’s subscript. The subscript notation is preferred over prefix(through:).

if let i = numbers.firstIndex(of: 40) {
    print(numbers[...i])
}
// Prints "[10, 20, 30, 40]"

Complexity: O(1)

See Also

Selecting Rows

func filter((MLDataTable.Row) -> Bool) -> [MLDataTable.Row]

Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.

func prefix(Int) -> Slice<MLDataTable.Rows>

Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.

func prefix(upTo: Int) -> Slice<MLDataTable.Rows>

Returns a subsequence from the start of the collection up to, but not including, the specified position.

func prefix(while: (MLDataTable.Row) -> Bool) -> Slice<MLDataTable.Rows>

Returns a subsequence containing the initial elements until predicate returns false and skipping the remaining elements.

func suffix(Int) -> Slice<MLDataTable.Rows>

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

func suffix(from: Int) -> Slice<MLDataTable.Rows>

Returns a subsequence from the specified position to the end of the collection.