Skip to content

Add conformances to LazySequenceProtocol and LazyCollectionProtocol #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 1, 2020
Merged
4 changes: 2 additions & 2 deletions Guides/Indexed.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ extension Collection {
```

`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.
its base type. `Indexed` also conforms to `LazySequenceProtocol` and
`LazyCollectionProtocol` when the base type conforms.

7 changes: 4 additions & 3 deletions Guides/Intersperse.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ extension Sequence {
```

The new `Intersperse` type represents the sequence when the separator is
inserted between each element. Intersperse conforms to Collection and
BidirectionalCollection when the base sequence conforms to Collection and
BidirectionalCollection respectively.
inserted between each element. `Intersperse` conforms to `Collection`,
`BidirectionalCollection`, `RandomAccessCollection`, `LazySequenceProtocol` and
`LazyCollectionProtocol` when the base sequence conforms to those respective
protocols.

### Complexity

Expand Down
4 changes: 2 additions & 2 deletions Guides/SlidingWindows.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ The first upper bound is computed eagerly because it determines if the collectio
```

The resulting `SlidingWindows` type is a collection, with conditional conformance to the
`BidirectionalCollection`, and `RandomAccessCollection` when the base collection
conforms.
`BidirectionalCollection`, `RandomAccessCollection`, and
`LazyCollectionProtocol` when the base collection conforms.

### Complexity

Expand Down
1 change: 1 addition & 0 deletions Sources/Algorithms/Indexed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ extension Indexed: BidirectionalCollection where Base: BidirectionalCollection {

extension Indexed: RandomAccessCollection where Base: RandomAccessCollection {}
extension Indexed: LazySequenceProtocol where Base: LazySequenceProtocol {}
extension Indexed: LazyCollectionProtocol where Base: LazyCollectionProtocol {}
extension Indexed: Equatable where Base: Equatable {}
extension Indexed: Hashable where Base: Hashable {}

Expand Down
6 changes: 6 additions & 0 deletions Sources/Algorithms/Intersperse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ extension Intersperse: BidirectionalCollection
extension Intersperse: RandomAccessCollection
where Base: RandomAccessCollection {}

extension Intersperse: LazySequenceProtocol
where Base: LazySequenceProtocol {}

extension Intersperse: LazyCollectionProtocol
where Base: LazyCollectionProtocol {}

extension Sequence {

/// Returns a sequence containing elements of this sequence with the given
Expand Down
2 changes: 2 additions & 0 deletions Sources/Algorithms/SlidingWindows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ extension SlidingWindows: BidirectionalCollection where Base: BidirectionalColle
}
}

extension SlidingWindows: LazySequenceProtocol where Base: LazySequenceProtocol {}
extension SlidingWindows: LazyCollectionProtocol where Base: LazyCollectionProtocol {}
extension SlidingWindows: RandomAccessCollection where Base: RandomAccessCollection {}
extension SlidingWindows: Equatable where Base: Equatable {}
extension SlidingWindows: Hashable where Base: Hashable, Base.Index: Hashable {}
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftAlgorithmsTests/ChunkedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ final class ChunkedTests: XCTestCase {
}

func testChunkedLazy() {
XCTAssertLazy(fruits.lazy.chunked(by: { $0.first == $1.first }))
XCTAssertLazy(fruits.lazy.chunked(on: { $0.first }))
XCTAssertLazySequence(fruits.lazy.chunked(by: { $0.first == $1.first }))
XCTAssertLazySequence(fruits.lazy.chunked(on: { $0.first }))
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftAlgorithmsTests/CombinationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ final class CombinationsTests: XCTestCase {
}

func testCombinationsLazy() {
XCTAssertLazy("ABC".lazy.combinations(ofCount: 1))
XCTAssertLazySequence("ABC".lazy.combinations(ofCount: 1))
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftAlgorithmsTests/CycleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ final class CycleTests: XCTestCase {
}

func testCycleLazy() {
XCTAssertLazy((1...4).lazy.cycled())
XCTAssertLazySequence((1...4).lazy.cycled())
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftAlgorithmsTests/IndexedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ final class IndexedTests: XCTestCase {
}

func testIndexedLazy() {
XCTAssertLazy("ABCD".lazy.indexed())
XCTAssertLazyCollection("ABCD".lazy.indexed())
}
}
5 changes: 5 additions & 0 deletions Tests/SwiftAlgorithmsTests/IntersperseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ final class IntersperseTests: XCTestCase {
XCTAssertEqualSequences(reversed, "E-D-C-B-A")
validateIndexTraversals(reversed)
}

func testIntersperseLazy() {
XCTAssertLazySequence((1...).prefix(0).lazy.interspersed(with: 0))
XCTAssertLazyCollection("ABCDE".lazy.interspersed(with: "-"))
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftAlgorithmsTests/PermutationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ final class PermutationsTests: XCTestCase {
}

func testPermutationsLazy() {
XCTAssertLazy("ABCD".lazy.permutations(ofCount: 2))
XCTAssertLazySequence("ABCD".lazy.permutations(ofCount: 2))
}
}
4 changes: 4 additions & 0 deletions Tests/SwiftAlgorithmsTests/SlidingWindowsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ final class SlidingWindowsTests: XCTestCase {
+ [.init(lowerBound: endIndex, upperBound: endIndex)]
})
}

func testWindowsLazy() {
XCTAssertLazyCollection([0, 1, 2, 3].lazy.slidingWindows(ofCount: 2))
}
}
3 changes: 2 additions & 1 deletion Tests/SwiftAlgorithmsTests/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func XCTAssertEqualSequences<S1: Sequence, S2: Sequence>(
try XCTAssert(expression1().elementsEqual(expression2(), by: areEquivalent), message(), file: file, line: line)
}

func XCTAssertLazy<S: LazySequenceProtocol>(_: S) {}
func XCTAssertLazySequence<S: LazySequenceProtocol>(_: S) {}
func XCTAssertLazyCollection<S: LazyCollectionProtocol>(_: S) {}

/// Tests that all index traversal methods behave as expected.
///
Expand Down