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

Conversation

cweider
Copy link

@cweider cweider commented Aug 8, 2025

Label the tuple produced by chunked(on:) (see #142) with subject and chunk. This aligns ChunkedOnCollection with the IndexedCollection, which specifies index and element as well as the built-in EnumeratedSequence (specifying offset and element).

This will also improve the legibility of code downstream of the said function. For example, when providing an identifier for SwiftUI's ForEach:

-ForEach(chunkedByDate, id: \.0) { date, items in
+ForEach(chunkedByDate, id: \.subject) { date, items in

The introduction of the label will have no effect on existing code, because the index-based address of the tuple component will remain unchanged and absent labels will be inferred. This said, the label choice should be considered carefully since subsequent relabelings will disrupt established code.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

Label the tuple produced by `chunked(on:)` with `subject` and
`chunk`. This will improve the legibility of code downstream of
the said function. For example, when providing an identifier
for SwiftUI's `ForEach`:

```
-ForEach(chunkedByDate, id: \.0) { date, items in
+ForEach(chunkedByDate, id: \.subject) { date, items in
```

This aligns `ChunkedOnCollection` with the `IndexedCollection`,
which specifies `index` and `element` as well as the built-in
`EnumeratedSequence` (specifying `offset` and `element`).

The introduction of the label will have no effect on existing code,
because the index-based address of the tuple component will remain
unchanged and absent labels will be inferred.
@cweider
Copy link
Author

cweider commented Aug 8, 2025

Aside: Ruby’s analogue, chunk, has no meaningful naming to take guidance from.

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.

@@ -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.

@@ -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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant