Skip to content

Commit 34573d4

Browse files
committed
Comment next and simplify its termination logic.
I love how the latter followed from the former. 😄
1 parent a82d266 commit 34573d4

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Sources/Algorithms/LazySplitSequence.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ extension LazySplitSequence.Iterator: IteratorProtocol {
5555
var currentElement = base.next()
5656
var subsequence: Element = []
5757

58+
// Add the next elements of the base sequence to this subsequence, until we
59+
// reach a separator, unless we've already split the maximum number of
60+
// times. In all cases, stop at the end of the base sequence.
5861
while currentElement != nil {
5962
if separatorCount < maxSplits && isSeparator(currentElement!) {
6063
separatorCount += 1
6164

6265
if omittingEmptySubsequences && subsequence.isEmpty {
66+
// Keep going if we don't want to return an empty subsequence.
6367
currentElement = base.next()
6468
continue
6569
} else {
@@ -71,22 +75,18 @@ extension LazySplitSequence.Iterator: IteratorProtocol {
7175
}
7276
}
7377

74-
if currentElement == nil {
75-
if sequenceLength < separatorCount + 1 {
76-
if !subsequence.isEmpty || !omittingEmptySubsequences {
77-
sequenceLength += 1
78-
return subsequence
79-
} else {
80-
return nil
81-
}
82-
} else {
83-
return nil
84-
}
78+
// We're done iterating when we've reached the end of the base sequence,
79+
// and we've either returned the maximum number of subsequences (one more
80+
// than the number of separators), or the only subsequence left to return is
81+
// empty and we're omitting those.
82+
if currentElement == nil
83+
&& (sequenceLength == separatorCount + 1
84+
|| omittingEmptySubsequences && subsequence.isEmpty) {
85+
return nil
86+
} else {
87+
sequenceLength += 1
88+
return subsequence
8589
}
86-
87-
sequenceLength += 1
88-
89-
return subsequence
9090
}
9191
}
9292

0 commit comments

Comments
 (0)