Files
Tim Vermeulen 53354f3f21 Add conditional LazySequenceProtocol conformance (#29)
* Add conditional LazySequenceProtocol conformance where appropriate

* Add laziness tests

* Mention conditional conformance to LazySequenceProtocol in the guides
2020-10-21 23:58:21 -05:00

36 lines
929 B
Markdown

# Indexed
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Indexed.swift) |
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/IndexedTests.swift)]
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)`:
```swift
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:
```swift
extension Collection {
func indexed() -> Indexed<Self>
}
```
`Indexed` scales from a collection up to a random-access collection, depending on
its base type. `Indexed` also conforms to `LazySequenceProtocol` when the base type
conforms.