Files
swift-algorithms/Guides/Indexed.md
T
2020-10-07 10:24:43 -05:00

841 B

Indexed

[Source | Tests]

The enumerated method, but pairing each element with its index instead of an incrementing integer counter.

This is essentially equivalent to zip(x.indices, x):

let numbers = [10, 20, 30, 40, 50]
var matchingIndices: Set<Int> = []
for (i, n) in numbers.indexed() {
    if n.isMultiple(of: 20) { 
        matchingIndices.insert(i) 
    }
}
// matchingIndices == [1, 3]

Detailed Design

The indexed method returns an Indexed type:

extension Sequence {
    func indexed() -> Indexed<Self>
}

Indexed scales from a sequence up to a random-access collection, depending on its base type.