Skip to content

Commit ab8c71d

Browse files
committed
Wrap a collection instead of a lazy collection.
Thanks, @timvermeulen, that does make the code more concise! I see that this is also the pattern used in `LazyMapSequence`, for example.
1 parent 31d26f7 commit ab8c71d

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Sources/Algorithms/LazySplitCollection.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
/// x.split(separator:maxSplits:omittingEmptySubsequences)
1919
///
2020
/// where `x` conforms to `LazyCollectionProtocol`.
21-
public struct LazySplitCollection<Base: LazyCollectionProtocol>
22-
where Base.Elements.Index == Base.Index {
21+
public struct LazySplitCollection<Base: Collection> {
2322
internal let base: Base
2423
internal let isSeparator: (Base.Element) -> Bool
2524
internal let maxSplits: Int
@@ -54,7 +53,7 @@ extension LazySplitCollection {
5453
}
5554

5655
extension LazySplitCollection.Iterator: IteratorProtocol {
57-
public typealias Element = Base.Elements.SubSequence
56+
public typealias Element = Base.SubSequence
5857

5958
public mutating func next() -> Element? {
6059
/// Separators mark the points where we want to split (cut in two) the base collection, removing
@@ -100,7 +99,7 @@ extension LazySplitCollection.Iterator: IteratorProtocol {
10099
/// number of separators we've encountered). This happens when the last element of the base
101100
/// collection is a separator. Return one last empty subsequence.
102101
sequenceLength += 1
103-
return base.elements[subsequenceStart..<subsequenceStart]
102+
return base[subsequenceStart..<subsequenceStart]
104103
} else {
105104
return nil
106105
}
@@ -133,7 +132,7 @@ extension LazySplitCollection.Iterator: IteratorProtocol {
133132
subsequenceStart = subsequenceEnd < base.endIndex ? base.index(after: subsequenceEnd) : base.endIndex
134133
}
135134

136-
return base.elements[subsequenceStart..<subsequenceEnd]
135+
return base[subsequenceStart..<subsequenceEnd]
137136
}
138137
}
139138

@@ -228,11 +227,11 @@ extension LazyCollectionProtocol where Elements.Index == Index {
228227
maxSplits: Int = Int.max,
229228
omittingEmptySubsequences: Bool = true,
230229
whereSeparator isSeparator: @escaping (Element) -> Bool
231-
) -> LazySplitCollection<Self> {
230+
) -> LazySplitCollection<Elements> {
232231
precondition(maxSplits >= 0, "Must take zero or more splits")
233232

234233
return LazySplitCollection(
235-
base: self,
234+
base: elements,
236235
isSeparator: isSeparator,
237236
maxSplits: maxSplits,
238237
omittingEmptySubsequences: omittingEmptySubsequences
@@ -318,11 +317,11 @@ extension LazyCollectionProtocol where Element: Equatable, Elements.Index == Ind
318317
separator: Element,
319318
maxSplits: Int = Int.max,
320319
omittingEmptySubsequences: Bool = true
321-
) -> LazySplitCollection<Self> {
320+
) -> LazySplitCollection<Elements> {
322321
precondition(maxSplits >= 0, "Must take zero or more splits")
323322

324323
return LazySplitCollection(
325-
base: self,
324+
base: elements,
326325
isSeparator: { $0 == separator },
327326
maxSplits: maxSplits,
328327
omittingEmptySubsequences: omittingEmptySubsequences

0 commit comments

Comments
 (0)