Skip to content

Commit b6c1da0

Browse files
author
Tim Vermeulen
committed
Add LazySequenceProtocol to StridingSequence, StridingCollection
1 parent 859e4bb commit b6c1da0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Sources/Algorithms/Stride.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ extension StridingSequence: Sequence {
112112
}
113113
}
114114

115+
extension StridingSequence: LazySequenceProtocol
116+
where Base: LazySequenceProtocol {}
117+
115118
/// A wrapper that strides over a base collection.
116119
public struct StridingCollection<Base: Collection> {
117120
@usableFromInline
@@ -273,4 +276,8 @@ extension StridingCollection: BidirectionalCollection
273276

274277
extension StridingCollection: RandomAccessCollection
275278
where Base: RandomAccessCollection {}
279+
280+
extension StridingCollection: LazySequenceProtocol, LazyCollectionProtocol
281+
where Base: LazySequenceProtocol {}
282+
276283
extension StridingCollection.Index: Hashable where Base.Index: Hashable {}

Tests/SwiftAlgorithmsTests/StrideTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ final class StridingTests: XCTestCase {
119119
validator.validate(string.striding(by: 10))
120120
}
121121
}
122-
123122

124123
func testOffsetBy() {
125124
let a = (0...100).striding(by: 22)
@@ -135,4 +134,10 @@ final class StridingTests: XCTestCase {
135134
let i = b.index(b.startIndex, offsetBy: 2)
136135
XCTAssertEqual(i, b.endIndex)
137136
}
137+
138+
func testLazy() {
139+
XCTAssertLazySequence(AnySequence(0..<100).lazy.striding(by: 3))
140+
XCTAssertLazySequence((0..<100).lazy.striding(by: 3))
141+
XCTAssertLazyCollection((0..<100).lazy.striding(by: 3))
142+
}
138143
}

0 commit comments

Comments
 (0)