Skip to content

Commit 93c8724

Browse files
authored
Add conformances to LazySequenceProtocol and LazyCollectionProtocol (#47)
* Conform Intersperse to LazySequenceProtocol when the base sequence does * Add conformance to LazyCollectionProtocol * Conform Indexed to LazyCollectionProtocol when Base conforms * Rename XCTAssertLazy to XCTAssertLazySequence * Add XCTAssertLazyCollection * Add lazy test for Intersperse * Update Indexed guide * Add lazy conformances to SlidingWindows * Update SlidingWindows guide
1 parent ad6dc7a commit 93c8724

14 files changed

+34
-14
lines changed

Guides/Indexed.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ extension Collection {
3030
```
3131

3232
`Indexed` scales from a collection up to a random-access collection, depending on
33-
its base type. `Indexed` also conforms to `LazySequenceProtocol` when the base type
34-
conforms.
33+
its base type. `Indexed` also conforms to `LazySequenceProtocol` and
34+
`LazyCollectionProtocol` when the base type conforms.
3535

Guides/Intersperse.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ extension Sequence {
3030
```
3131

3232
The new `Intersperse` type represents the sequence when the separator is
33-
inserted between each element. Intersperse conforms to Collection and
34-
BidirectionalCollection when the base sequence conforms to Collection and
35-
BidirectionalCollection respectively.
33+
inserted between each element. `Intersperse` conforms to `Collection`,
34+
`BidirectionalCollection`, `RandomAccessCollection`, `LazySequenceProtocol` and
35+
`LazyCollectionProtocol` when the base sequence conforms to those respective
36+
protocols.
3637

3738
### Complexity
3839

Guides/SlidingWindows.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ The first upper bound is computed eagerly because it determines if the collectio
3737
```
3838

3939
The resulting `SlidingWindows` type is a collection, with conditional conformance to the
40-
`BidirectionalCollection`, and `RandomAccessCollection` when the base collection
41-
conforms.
40+
`BidirectionalCollection`, `RandomAccessCollection`, and
41+
`LazyCollectionProtocol` when the base collection conforms.
4242

4343
### Complexity
4444

Sources/Algorithms/Indexed.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extension Indexed: BidirectionalCollection where Base: BidirectionalCollection {
7070

7171
extension Indexed: RandomAccessCollection where Base: RandomAccessCollection {}
7272
extension Indexed: LazySequenceProtocol where Base: LazySequenceProtocol {}
73+
extension Indexed: LazyCollectionProtocol where Base: LazyCollectionProtocol {}
7374
extension Indexed: Equatable where Base: Equatable {}
7475
extension Indexed: Hashable where Base: Hashable {}
7576

Sources/Algorithms/Intersperse.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ extension Intersperse: BidirectionalCollection
152152
extension Intersperse: RandomAccessCollection
153153
where Base: RandomAccessCollection {}
154154

155+
extension Intersperse: LazySequenceProtocol
156+
where Base: LazySequenceProtocol {}
157+
158+
extension Intersperse: LazyCollectionProtocol
159+
where Base: LazyCollectionProtocol {}
160+
155161
extension Sequence {
156162

157163
/// Returns a sequence containing elements of this sequence with the given

Sources/Algorithms/SlidingWindows.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ extension SlidingWindows: BidirectionalCollection where Base: BidirectionalColle
299299
}
300300
}
301301

302+
extension SlidingWindows: LazySequenceProtocol where Base: LazySequenceProtocol {}
303+
extension SlidingWindows: LazyCollectionProtocol where Base: LazyCollectionProtocol {}
302304
extension SlidingWindows: RandomAccessCollection where Base: RandomAccessCollection {}
303305
extension SlidingWindows: Equatable where Base: Equatable {}
304306
extension SlidingWindows: Hashable where Base: Hashable, Base.Index: Hashable {}

Tests/SwiftAlgorithmsTests/ChunkedTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ final class ChunkedTests: XCTestCase {
7373
}
7474

7575
func testChunkedLazy() {
76-
XCTAssertLazy(fruits.lazy.chunked(by: { $0.first == $1.first }))
77-
XCTAssertLazy(fruits.lazy.chunked(on: { $0.first }))
76+
XCTAssertLazySequence(fruits.lazy.chunked(by: { $0.first == $1.first }))
77+
XCTAssertLazySequence(fruits.lazy.chunked(on: { $0.first }))
7878
}
7979
}

Tests/SwiftAlgorithmsTests/CombinationsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ final class CombinationsTests: XCTestCase {
4040
}
4141

4242
func testCombinationsLazy() {
43-
XCTAssertLazy("ABC".lazy.combinations(ofCount: 1))
43+
XCTAssertLazySequence("ABC".lazy.combinations(ofCount: 1))
4444
}
4545
}

Tests/SwiftAlgorithmsTests/CycleTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ final class CycleTests: XCTestCase {
4141
}
4242

4343
func testCycleLazy() {
44-
XCTAssertLazy((1...4).lazy.cycled())
44+
XCTAssertLazySequence((1...4).lazy.cycled())
4545
}
4646
}

Tests/SwiftAlgorithmsTests/IndexedTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ final class IndexedTests: XCTestCase {
2929
}
3030

3131
func testIndexedLazy() {
32-
XCTAssertLazy("ABCD".lazy.indexed())
32+
XCTAssertLazyCollection("ABCD".lazy.indexed())
3333
}
3434
}

Tests/SwiftAlgorithmsTests/IntersperseTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ final class IntersperseTests: XCTestCase {
5757
XCTAssertEqualSequences(reversed, "E-D-C-B-A")
5858
validateIndexTraversals(reversed)
5959
}
60+
61+
func testIntersperseLazy() {
62+
XCTAssertLazySequence((1...).prefix(0).lazy.interspersed(with: 0))
63+
XCTAssertLazyCollection("ABCDE".lazy.interspersed(with: "-"))
64+
}
6065
}

Tests/SwiftAlgorithmsTests/PermutationsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ final class PermutationsTests: XCTestCase {
7171
}
7272

7373
func testPermutationsLazy() {
74-
XCTAssertLazy("ABCD".lazy.permutations(ofCount: 2))
74+
XCTAssertLazySequence("ABCD".lazy.permutations(ofCount: 2))
7575
}
7676
}

Tests/SwiftAlgorithmsTests/SlidingWindowsTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,8 @@ final class SlidingWindowsTests: XCTestCase {
106106
+ [.init(lowerBound: endIndex, upperBound: endIndex)]
107107
})
108108
}
109+
110+
func testWindowsLazy() {
111+
XCTAssertLazyCollection([0, 1, 2, 3].lazy.slidingWindows(ofCount: 2))
112+
}
109113
}

Tests/SwiftAlgorithmsTests/TestUtilities.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func XCTAssertEqualSequences<S1: Sequence, S2: Sequence>(
6565
try XCTAssert(expression1().elementsEqual(expression2(), by: areEquivalent), message(), file: file, line: line)
6666
}
6767

68-
func XCTAssertLazy<S: LazySequenceProtocol>(_: S) {}
68+
func XCTAssertLazySequence<S: LazySequenceProtocol>(_: S) {}
69+
func XCTAssertLazyCollection<S: LazyCollectionProtocol>(_: S) {}
6970

7071
/// Tests that all index traversal methods behave as expected.
7172
///

0 commit comments

Comments
 (0)