swift-algorithms icon indicating copy to clipboard operation
swift-algorithms copied to clipboard

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

Open cweider opened this issue 5 months ago • 3 comments

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 minimal impact on existing code, because the index-based address of the tuple component will remain unchanged and absent labels will be inferred. There may be extant usages of chunked(on:) where labels are specified and do rely on their absence in the current API, but these should be expected to be extremely rare and produce a compilation error that can be addressed easily. This said, the label choice should be considered carefully since subsequent relabelings will be certain to disrupt established code.

Checklist

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

cweider avatar Aug 08 '25 21:08 cweider

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

cweider avatar Aug 08 '25 21:08 cweider

The introduction of the label will have no effect on existing code

This is not strictly true because of Swift's tuple shuffle behavior, although it is unlikely to break correct working code.

xwu avatar Aug 09 '25 17:08 xwu

@xwu, you are correct - amended the PR message to note this.

cweider avatar Aug 12 '25 00:08 cweider