Skip to content

Add labels to the tuples produced by chunked(on:) #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/Algorithms/Chunked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public struct ChunkedOnCollection<Base: Collection, Subject: Equatable> {

extension ChunkedOnCollection: Collection {
public typealias Index = ChunkedByCollection<Base, Subject>.Index
public typealias Element = (subject: Subject, chunk: Base.SubSequence)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cf. IndexedCollection:

extension IndexedCollection: Collection {
/// The element type for an `IndexedCollection` collection.
public typealias Element = (index: Base.Index, element: Base.Element)


@inlinable
public var startIndex: Index {
Expand All @@ -174,7 +175,7 @@ extension ChunkedOnCollection: Collection {
}

@inlinable
public subscript(position: Index) -> (Subject, Base.SubSequence) {
public subscript(position: Index) -> Element {
let subsequence = chunked[position]
// swift-format-ignore: NeverForceUnwrap
// Chunks are never empty, so `.first!` is safe.
Expand Down Expand Up @@ -509,7 +510,7 @@ extension Collection {
@inlinable
public func chunked<Subject: Equatable>(
on projection: (Element) throws -> Subject
) rethrows -> [(Subject, SubSequence)] {
) rethrows -> [ChunkedOnCollection<Self, Subject>.Element] {
Copy link
Author

@cweider cweider Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation below sidesteps ChunkedOnCollection, but aliasing seems sensible given Array-ness is the only material difference between this and the LazySequenceProtocol function.

guard !isEmpty else { return [] }
var result: [(Subject, SubSequence)] = []

Expand Down
10 changes: 10 additions & 0 deletions Tests/SwiftAlgorithmsTests/ChunkedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ final class ChunkedTests: XCTestCase {
IndexValidator().validate(lazyChunks)
}

func testChunkedOnLabels() {
let arrayChunks: Array = fruits.chunked(on: { $0.first })
XCTAssert(arrayChunks.first!.0 == arrayChunks.first!.subject)
XCTAssert(arrayChunks.first!.1 == arrayChunks.first!.chunk)

let lazyChunks = fruits.lazy.chunked(on: { $0.first })
XCTAssert(lazyChunks.first!.0 == lazyChunks.first!.subject)
XCTAssert(lazyChunks.first!.1 == lazyChunks.first!.chunk)
}
Copy link
Author

@cweider cweider Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is a bit artificial – correctness is known statically. The best justification is that inhibits subsequent change to the labels. I welcome your thoughts here.


func testChunkedBy() {
validateFruitChunks(fruits.chunked(by: { $0.first == $1.first }))

Expand Down