-
Notifications
You must be signed in to change notification settings - Fork 449
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
base: main
Are you sure you want to change the base?
Conversation
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.
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] { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cf. IndexedCollection
:
swift-algorithms/Sources/Algorithms/Indexed.swift
Lines 25 to 27 in 0b70cac
extension IndexedCollection: Collection { | |
/// The element type for an `IndexedCollection` collection. | |
public typealias Element = (index: Base.Index, element: Base.Element) |
Label the tuple produced by
chunked(on:)
(see #142) withsubject
andchunk
. This alignsChunkedOnCollection
with theIndexedCollection
, which specifiesindex
andelement
as well as the built-inEnumeratedSequence
(specifyingoffset
andelement
).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 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