Skip to content

Commit 0a6bcc3

Browse files
committed
compute the stride only when the next element is requested
1 parent 92b2846 commit 0a6bcc3

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Sources/Algorithms/Stride.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ extension Stride: Sequence {
5656

5757
var iterator: Base.Iterator
5858
let stride: Int
59+
var striding: Bool = false
5960

6061
public mutating func next() -> Base.Element? {
61-
defer {
62-
for _ in 0..<(stride - 1) {
63-
guard iterator.next() != nil else { break }
64-
}
62+
guard striding else {
63+
striding = true
64+
return iterator.next()
65+
}
66+
for _ in 0..<stride - 1 {
67+
guard iterator.next() != nil else { break }
6568
}
6669
return iterator.next()
6770
}

0 commit comments

Comments
 (0)