mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-26 04:27:48 +00:00
Add conditional LazySequenceProtocol conformance (#29)
* Add conditional LazySequenceProtocol conformance where appropriate * Add laziness tests * Mention conditional conformance to LazySequenceProtocol in the guides
This commit is contained in:
1 parent
8ca636d702
commit
53354f3f21
17 files changed
+55
-19
No files matched your search
+4
-4
@@ -29,12 +29,12 @@ extension Sequence {
|
||||
public func chained<S: Sequence>(with other: S) -> Concatenation<Self, S>
|
||||
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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+6
-4
@@ -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
|
||||
|
||||
|
||||
+2
-1
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
|
||||
@@ -48,6 +48,8 @@ extension Cycle: Sequence {
|
||||
}
|
||||
}
|
||||
|
||||
extension Cycle: LazySequenceProtocol where Base: LazySequenceProtocol {}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// cycled()
|
||||
//===----------------------------------------------------------------------===//
|
||||
@@ -81,13 +83,7 @@ extension Collection {
|
||||
public func cycled() -> Cycle<Self> {
|
||||
Cycle(base: self)
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// repeated(count:)
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension Collection {
|
||||
|
||||
/// Returns a sequence that repeats the elements of this collection the
|
||||
/// specified number of times.
|
||||
///
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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]))
|
||||
}
|
||||
}
|
||||
@@ -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 }))
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -65,3 +65,4 @@ func XCTAssertEqualSequences<S1: Sequence, S2: Sequence>(
|
||||
try XCTAssert(expression1().elementsEqual(expression2(), by: areEquivalent), message(), file: file, line: line)
|
||||
}
|
||||
|
||||
func XCTAssertLazy<S: LazySequenceProtocol>(_: S) {}
|
||||
Reference in new issue
Block a user