From 53354f3f217f6c6e67ff152d7b83d5ed6ca0f0c6 Mon Sep 17 00:00:00 2001 From: Tim Vermeulen Date: Thu, 22 Oct 2020 06:58:21 +0200 Subject: [PATCH] Add conditional LazySequenceProtocol conformance (#29) * Add conditional LazySequenceProtocol conformance where appropriate * Add laziness tests * Mention conditional conformance to LazySequenceProtocol in the guides --- Guides/Chain.md | 8 ++++---- Guides/Combinations.md | 3 ++- Guides/Cycle.md | 10 ++++++---- Guides/Indexed.md | 3 ++- Guides/Permutations.md | 3 ++- Sources/Algorithms/Chain.swift | 1 + Sources/Algorithms/Combinations.swift | 1 + Sources/Algorithms/Cycle.swift | 10 +++------- Sources/Algorithms/Indexed.swift | 5 +++++ Sources/Algorithms/Permutations.swift | 4 +++- Tests/SwiftAlgorithmsTests/ChainTests.swift | 4 ++++ Tests/SwiftAlgorithmsTests/ChunkedTests.swift | 5 +++++ Tests/SwiftAlgorithmsTests/CombinationsTests.swift | 4 ++++ Tests/SwiftAlgorithmsTests/CycleTests.swift | 4 ++++ Tests/SwiftAlgorithmsTests/IndexedTests.swift | 4 ++++ Tests/SwiftAlgorithmsTests/PermutationsTests.swift | 4 ++++ Tests/SwiftAlgorithmsTests/TestUtilities.swift | 1 + 17 files changed, 55 insertions(+), 19 deletions(-) diff --git a/Guides/Chain.md b/Guides/Chain.md index 5cdf76f..24741b5 100644 --- a/Guides/Chain.md +++ b/Guides/Chain.md @@ -29,12 +29,12 @@ extension Sequence { public func chained(with other: S) -> Concatenation where Element == S.Element } - ``` -The resulting `Chain` type is a sequence, with conditional conformance to the -`Collection`, `BidirectionalCollection`, and `RandomAccessCollection` when both -the first and second arguments conform. +The resulting `Chain` type is a sequence, with conditional conformance to +`Collection`, `BidirectionalCollection`, and `RandomAccessCollection` when both +the first and second arguments conform. `Chain` also conforms to +`LazySequenceProtocol` when the first argument conforms. ### Naming diff --git a/Guides/Combinations.md b/Guides/Combinations.md index 4823a43..6f0e186 100644 --- a/Guides/Combinations.md +++ b/Guides/Combinations.md @@ -51,7 +51,8 @@ Since the `Combinations` type needs to store an array of the collection’s indices and mutate the array to generate each permutation, `Combinations` only has `Sequence` conformance. Adding `Collection` conformance would require storing the array in the index type, which would in turn lead to copying the -array at every index advancement. +array at every index advancement. `Combinations` does conform to +`LazySequenceProtocol` when the base type conforms. ### Complexity diff --git a/Guides/Cycle.md b/Guides/Cycle.md index 2324a91..727c603 100644 --- a/Guides/Cycle.md +++ b/Guides/Cycle.md @@ -33,10 +33,12 @@ extension Collection { ``` The new `Cycle` type is a sequence only, given that the `Collection` protocol -design makes infinitely large types impossible/impractical. Note that despite -its name, the returned `FlattenSequence` will always have `Collection` -conformance, and will have `BidirectionalCollection` conformance when called on -a bidirectional collection. +design makes infinitely large types impossible/impractical. `Cycle` also +conforms to `LazySequenceProtocol` when the base type conforms. + +Note that despite its name, the returned `FlattenSequence` will always have +`Collection` conformance, and will have `BidirectionalCollection` conformance +when called on a bidirectional collection. ### Complexity diff --git a/Guides/Indexed.md b/Guides/Indexed.md index 11eb52c..e1a6a11 100644 --- a/Guides/Indexed.md +++ b/Guides/Indexed.md @@ -30,5 +30,6 @@ extension Collection { ``` `Indexed` scales from a collection up to a random-access collection, depending on -its base type. +its base type. `Indexed` also conforms to `LazySequenceProtocol` when the base type +conforms. diff --git a/Guides/Permutations.md b/Guides/Permutations.md index 6529593..ccfbd27 100644 --- a/Guides/Permutations.md +++ b/Guides/Permutations.md @@ -75,7 +75,8 @@ Since the `Permutations` type needs to store an array of the collection’s indices and mutate the array to generate each permutation, `Permutations` only has `Sequence` conformance. Adding `Collection` conformance would require storing the array in the index type, which would in turn lead to copying the -array at every index advancement. +array at every index advancement. `Combinations` does conform to +`LazySequenceProtocol` when the base type conforms. ### Complexity diff --git a/Sources/Algorithms/Chain.swift b/Sources/Algorithms/Chain.swift index 4b0228a..9e6e827 100644 --- a/Sources/Algorithms/Chain.swift +++ b/Sources/Algorithms/Chain.swift @@ -272,6 +272,7 @@ extension Chain: BidirectionalCollection extension Chain: RandomAccessCollection where Base1: RandomAccessCollection, Base2: RandomAccessCollection {} +extension Chain: LazySequenceProtocol where Base1: LazySequenceProtocol {} extension Chain: Equatable where Base1: Equatable, Base2: Equatable {} extension Chain: Hashable where Base1: Hashable, Base2: Hashable {} diff --git a/Sources/Algorithms/Combinations.swift b/Sources/Algorithms/Combinations.swift index ab7413e..b35fb1d 100644 --- a/Sources/Algorithms/Combinations.swift +++ b/Sources/Algorithms/Combinations.swift @@ -122,6 +122,7 @@ extension Combinations: Sequence { } } +extension Combinations: LazySequenceProtocol where Base: LazySequenceProtocol {} extension Combinations: Equatable where Base: Equatable {} extension Combinations: Hashable where Base: Hashable {} diff --git a/Sources/Algorithms/Cycle.swift b/Sources/Algorithms/Cycle.swift index 44b0883..52017fa 100644 --- a/Sources/Algorithms/Cycle.swift +++ b/Sources/Algorithms/Cycle.swift @@ -48,6 +48,8 @@ extension Cycle: Sequence { } } +extension Cycle: LazySequenceProtocol where Base: LazySequenceProtocol {} + //===----------------------------------------------------------------------===// // cycled() //===----------------------------------------------------------------------===// @@ -81,13 +83,7 @@ extension Collection { public func cycled() -> Cycle { Cycle(base: self) } -} - -//===----------------------------------------------------------------------===// -// repeated(count:) -//===----------------------------------------------------------------------===// - -extension Collection { + /// Returns a sequence that repeats the elements of this collection the /// specified number of times. /// diff --git a/Sources/Algorithms/Indexed.swift b/Sources/Algorithms/Indexed.swift index c7d0801..2e294b9 100644 --- a/Sources/Algorithms/Indexed.swift +++ b/Sources/Algorithms/Indexed.swift @@ -69,9 +69,14 @@ extension Indexed: BidirectionalCollection where Base: BidirectionalCollection { } extension Indexed: RandomAccessCollection where Base: RandomAccessCollection {} +extension Indexed: LazySequenceProtocol where Base: LazySequenceProtocol {} extension Indexed: Equatable where Base: Equatable {} extension Indexed: Hashable where Base: Hashable {} +//===----------------------------------------------------------------------===// +// indexed() +//===----------------------------------------------------------------------===// + extension Collection { /// Returns a collection of pairs *(i, x)*, where *i* represents an index of /// the collection, and *x* represents an element. diff --git a/Sources/Algorithms/Permutations.swift b/Sources/Algorithms/Permutations.swift index ca851e3..905fdcc 100644 --- a/Sources/Algorithms/Permutations.swift +++ b/Sources/Algorithms/Permutations.swift @@ -139,6 +139,8 @@ extension Permutations: Sequence { } } +extension Permutations: LazySequenceProtocol where Base: LazySequenceProtocol {} + //===----------------------------------------------------------------------===// // nextPermutation(by:) //===----------------------------------------------------------------------===// @@ -188,7 +190,7 @@ extension MutableCollection } //===----------------------------------------------------------------------===// -// permutations(count:) +// permutations(ofCount:) //===----------------------------------------------------------------------===// extension Collection { diff --git a/Tests/SwiftAlgorithmsTests/ChainTests.swift b/Tests/SwiftAlgorithmsTests/ChainTests.swift index 67a0464..915c460 100644 --- a/Tests/SwiftAlgorithmsTests/ChainTests.swift +++ b/Tests/SwiftAlgorithmsTests/ChainTests.swift @@ -136,4 +136,8 @@ final class ChainTests: XCTestCase { XCTAssertEqual(chain.distance(from: start, to: end), distance) } } + + func testChainLazy() { + XCTAssertLazy([1, 2, 3].lazy.chained(with: [4, 5, 6])) + } } diff --git a/Tests/SwiftAlgorithmsTests/ChunkedTests.swift b/Tests/SwiftAlgorithmsTests/ChunkedTests.swift index 66682b0..1b8f157 100644 --- a/Tests/SwiftAlgorithmsTests/ChunkedTests.swift +++ b/Tests/SwiftAlgorithmsTests/ChunkedTests.swift @@ -71,4 +71,9 @@ final class ChunkedTests: XCTestCase { let lazyChunks = fruits.lazy.chunked(by: { $0.first == $1.first }) validateFruitChunks(lazyChunks) } + + func testChunkedLazy() { + XCTAssertLazy(fruits.lazy.chunked(by: { $0.first == $1.first })) + XCTAssertLazy(fruits.lazy.chunked(on: { $0.first })) + } } diff --git a/Tests/SwiftAlgorithmsTests/CombinationsTests.swift b/Tests/SwiftAlgorithmsTests/CombinationsTests.swift index 77cab02..a9c880e 100644 --- a/Tests/SwiftAlgorithmsTests/CombinationsTests.swift +++ b/Tests/SwiftAlgorithmsTests/CombinationsTests.swift @@ -38,4 +38,8 @@ final class CombinationsTests: XCTestCase { XCTAssertEqualSequences([], "".combinations(ofCount: 5)) XCTAssertEqualSequences([], "ABCD".combinations(ofCount: 5)) } + + func testCombinationsLazy() { + XCTAssertLazy("ABC".lazy.combinations(ofCount: 1)) + } } diff --git a/Tests/SwiftAlgorithmsTests/CycleTests.swift b/Tests/SwiftAlgorithmsTests/CycleTests.swift index 181ec69..ffc7bad 100644 --- a/Tests/SwiftAlgorithmsTests/CycleTests.swift +++ b/Tests/SwiftAlgorithmsTests/CycleTests.swift @@ -39,4 +39,8 @@ final class CycleTests: XCTestCase { let empty2 = Array("Hello".cycled(times: 0)) XCTAssert(empty2.isEmpty) } + + func testCycleLazy() { + XCTAssertLazy((1...4).lazy.cycled()) + } } diff --git a/Tests/SwiftAlgorithmsTests/IndexedTests.swift b/Tests/SwiftAlgorithmsTests/IndexedTests.swift index 7e2b7ba..2c71b18 100644 --- a/Tests/SwiftAlgorithmsTests/IndexedTests.swift +++ b/Tests/SwiftAlgorithmsTests/IndexedTests.swift @@ -27,4 +27,8 @@ final class IndexedTests: XCTestCase { let indexOfI = si.last(where: { $0.element == "I" })!.index XCTAssertEqual("I", s[indexOfI]) } + + func testIndexedLazy() { + XCTAssertLazy("ABCD".lazy.indexed()) + } } diff --git a/Tests/SwiftAlgorithmsTests/PermutationsTests.swift b/Tests/SwiftAlgorithmsTests/PermutationsTests.swift index 1085d7e..d6d9ea8 100644 --- a/Tests/SwiftAlgorithmsTests/PermutationsTests.swift +++ b/Tests/SwiftAlgorithmsTests/PermutationsTests.swift @@ -69,4 +69,8 @@ final class PermutationsTests: XCTestCase { while numbers.nextPermutation() {} XCTAssertEqual([1, 2, 3, 4, 5, 6, 7], numbers) } + + func testPermutationsLazy() { + XCTAssertLazy("ABCD".lazy.permutations(ofCount: 2)) + } } diff --git a/Tests/SwiftAlgorithmsTests/TestUtilities.swift b/Tests/SwiftAlgorithmsTests/TestUtilities.swift index d736696..27c9c41 100644 --- a/Tests/SwiftAlgorithmsTests/TestUtilities.swift +++ b/Tests/SwiftAlgorithmsTests/TestUtilities.swift @@ -65,3 +65,4 @@ func XCTAssertEqualSequences( try XCTAssert(expression1().elementsEqual(expression2(), by: areEquivalent), message(), file: file, line: line) } +func XCTAssertLazy(_: S) {}