Add labels to the tuples produced by chunked(on:)#259
Add labels to the tuples produced by chunked(on:)#259cweider wants to merge 1 commit intoapple:mainfrom
chunked(on:)#259Conversation
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.
|
Aside: Ruby’s analogue, |
| let lazyChunks = fruits.lazy.chunked(on: { $0.first }) | ||
| XCTAssert(lazyChunks.first!.0 == lazyChunks.first!.subject) | ||
| XCTAssert(lazyChunks.first!.1 == lazyChunks.first!.chunk) | ||
| } |
There was a problem hiding this comment.
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.
| public func chunked<Subject: Equatable>( | ||
| on projection: (Element) throws -> Subject | ||
| ) rethrows -> [(Subject, SubSequence)] { | ||
| ) rethrows -> [ChunkedOnCollection<Self, Subject>.Element] { |
There was a problem hiding this comment.
The implementation below sidesteps ChunkedOnCollection, but aliasing seems sensible given Array-ness is the only material difference between this and the LazySequenceProtocol function.
|
|
||
| extension ChunkedOnCollection: Collection { | ||
| public typealias Index = ChunkedByCollection<Base, Subject>.Index | ||
| public typealias Element = (subject: Subject, chunk: Base.SubSequence) |
There was a problem hiding this comment.
cf. IndexedCollection:
swift-algorithms/Sources/Algorithms/Indexed.swift
Lines 25 to 27 in 0b70cac
This is not strictly true because of Swift's tuple shuffle behavior, although it is unlikely to break correct working code. |
|
@xwu, you are correct - amended the PR message to note this. |
Label the tuple produced by
chunked(on:)(see #142) withsubjectandchunk. This alignsChunkedOnCollectionwith theIndexedCollection, which specifiesindexandelementas well as the built-inEnumeratedSequence(specifyingoffsetandelement).This will also improve the legibility of code downstream of the said function. For example, when providing an identifier for SwiftUI's
ForEach: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