apple / swift-algorithms

Commonly used sequence and collection algorithms for Swift
Apache License 2.0
5.92k stars 440 forks source link

Make chunked(on:) include subject value in Element type #142

Closed natecook1000 closed 3 years ago

natecook1000 commented 3 years ago

This PR changes the result of calling chunked(on:) to [(Subject, SubSequence)], and adds a new collection type as the result of lazy.chunked(on:) with a matching element type. Including the projected Subject is often useful when chunking on a projected value like this.

The new versions of chunked(on:) looks like this:

extension Collection {
  public func chunked<Subject: Equatable>(
    on projection: (Element) throws -> Subject
  ) rethrows -> [(Subject, SubSequence)]
}

extension LazyCollectionProtocol {
  public func chunked<Subject: Equatable>(
    on projection: (Element) throws -> Subject
  ) rethrows -> ChunkedOn<Self, Subject>
}

I've also changed the name Chunked to be ChunkedBy, since this PR introduces ChunkedOn.

Checklist

natecook1000 commented 3 years ago

@swift-ci Please test