diff --git a/Sources/Algorithms/Chunked.swift b/Sources/Algorithms/Chunked.swift index 30db1607..883ecf36 100644 --- a/Sources/Algorithms/Chunked.swift +++ b/Sources/Algorithms/Chunked.swift @@ -77,9 +77,13 @@ extension ChunkedBy: LazyCollectionProtocol { /// at the given index. @inlinable internal func endOfChunk(startingAt start: Base.Index) -> Base.Index { - let subject = projection(base[start]) - return base[base.index(after: start)...] - .endOfPrefix(while: { belongInSameGroup(subject, projection($0)) }) + var subject = projection(base[start]) + + return base[base.index(after: start)...].endOfPrefix(while: { element in + let nextSubject = projection(element) + defer { subject = nextSubject } + return belongInSameGroup(subject, nextSubject) + }) } @inlinable @@ -118,11 +122,13 @@ extension ChunkedBy: BidirectionalCollection @inlinable internal func startOfChunk(endingAt end: Base.Index) -> Base.Index { let indexBeforeEnd = base.index(before: end) + var subject = projection(base[indexBeforeEnd]) - // Get the projected value of the last element in the range ending at `end`. - let subject = projection(base[indexBeforeEnd]) - return base[..