Skip to content

Commit 28bbed8

Browse files
committed
Add implementation for Sequence iterator
1 parent 6f7148d commit 28bbed8

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

Guides/Stride.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
A type that steps over a collection’s elements by the specified amount.
77

8-
This is available through the `striding(by:)` method on any `Collection`.
8+
This is available through the `striding(by:)` method on any `Sequence`.
99

1010
```swift
1111
(0...10).striding(by: 2) // == [0, 2, 4, 6, 8, 10]
@@ -18,11 +18,11 @@ The stride amount must be a positive value.
1818

1919
## Detailed Design
2020

21-
The `striding(by:)` method is declared as a `Collection` extension, and returns a
21+
The `striding(by:)` method is declared as a `Sequence` extension, and returns a
2222
`Stride` type:
2323

2424
```swift
25-
extension Collection {
25+
extension Sequence {
2626
public func striding(by step: Int) -> Stride<Self>
2727
}
2828
```

Sources/Algorithms/Stride.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// striding(by:)
1414
//===----------------------------------------------------------------------===//
1515

16-
extension Collection {
16+
extension Sequence {
1717
/// Returns a collection stepping through the elements every `step` starting
1818
/// at the first value. Any remainders of the stride will be trimmed.
1919
///
@@ -32,7 +32,7 @@ extension Collection {
3232
}
3333
}
3434

35-
public struct Stride<Base: Collection> {
35+
public struct Stride<Base: Sequence> {
3636

3737
let base: Base
3838
let stride: Int
@@ -50,7 +50,29 @@ extension Stride {
5050
}
5151
}
5252

53-
extension Stride: Collection {
53+
extension Stride: Sequence {
54+
55+
public struct Iterator: IteratorProtocol {
56+
57+
var iterator: Base.Iterator
58+
let stride: Int
59+
60+
public mutating func next() -> Base.Element? {
61+
defer {
62+
for _ in 0..<(stride - 1) {
63+
guard iterator.next() != nil else { break }
64+
}
65+
}
66+
return iterator.next()
67+
}
68+
}
69+
70+
public func makeIterator() -> Stride<Base>.Iterator {
71+
Iterator(iterator: base.makeIterator(), stride: stride)
72+
}
73+
}
74+
75+
extension Stride: Collection where Base: Collection {
5476

5577
public struct Index: Comparable {
5678

Tests/SwiftAlgorithmsTests/StrideTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Algorithms
1515
final class StridingTests: XCTestCase {
1616

1717
func testStride() {
18-
let a = (0...10)
18+
let a = 0...10
1919
XCTAssertEqualSequences(a.striding(by: 1), (0...10))
2020
XCTAssertEqualSequences(a.striding(by: 2), [0, 2, 4, 6, 8, 10])
2121
XCTAssertEqualSequences(a.striding(by: 3), [0, 3, 6, 9])
@@ -48,7 +48,7 @@ final class StridingTests: XCTestCase {
4848
XCTAssertEqual(a[i], 2)
4949
a.formIndex(before: &i)
5050
XCTAssertEqual(a[i], 0)
51-
// a.formIndex(before: &i) // Precondition failed: Advancing past start index
51+
// a.formIndex(before: &i) // Precondition failed: Incrementing past start index
5252
// a.index(after: a.endIndex) // Precondition failed: Advancing past end index
5353
}
5454

0 commit comments

Comments
 (0)