-
Notifications
You must be signed in to change notification settings - Fork 450
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
@inlinable | ||
public var startIndex: Index { | ||
|
@@ -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. | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. The implementation below sidesteps |
||
guard !isEmpty else { return [] } | ||
var result: [(Subject, SubSequence)] = [] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 })) | ||
|
||
|
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