Skip to content
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

Add joined(by:) #138

Merged
merged 4 commits into from
Jun 1, 2021
Merged

Add joined(by:) #138

merged 4 commits into from
Jun 1, 2021

Conversation

timvermeulen
Copy link
Contributor

@timvermeulen timvermeulen commented May 3, 2021

This PR adds 4 ways of joining sequences of sequences with a separator, two of which let you base the separator on the sequences right before and after it:

extension Sequence where Element: Sequence {
  public func joined(by separator: Element.Element)
    -> JoinedBySequence<Self, CollectionOfOne<Element.Element>>
  
  public func joined<Separator>(
    by separator: Separator
  ) -> JoinedBySequence<Self, Separator>
    where Separator: Collection, Separator.Element == Element.Element
  
  public func joined(
    by separator: (Element, Element) throws -> Element.Element
  ) rethrows -> [Element.Element]
  
  public func joined<Separator>(
    by separator: (Element, Element) throws -> Separator
  ) rethrows -> [Element.Element]
    where Separator: Sequence, Separator.Element == Element.Element
}

Also adds the following building blocks internally:

  • Either and EitherSequence
  • FlattenCollection that flattens a collection of collections without separators
  • InterspersedMap that maps the sequence's elements and intersperses them with new elements at the same time

Checklist

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

Copy link
Contributor

@kylemacomber kylemacomber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top notch work!

So much great stuff here, which I'm sure we'll be able to mine for future additions!

}

extension InterspersedMap: Sequence {
public struct Iterator: IteratorProtocol {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this public access control is accidental?

Suggested change
public struct Iterator: IteratorProtocol {
internal struct Iterator: IteratorProtocol {

}

@inlinable
public mutating func next() -> Result? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public mutating func next() -> Result? {
internal mutating func next() -> Result? {

}

@inlinable
public func makeIterator() -> Iterator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public func makeIterator() -> Iterator {
internal func makeIterator() -> Iterator {

}

extension EitherSequence: Sequence {
public struct Iterator: IteratorProtocol {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since enum decl is internal, this may also be internal too?

@timvermeulen timvermeulen marked this pull request as ready for review May 25, 2021 17:35
@timvermeulen timvermeulen requested a review from natecook1000 May 25, 2021 18:14
Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, @timvermeulen! 🎉

@natecook1000
Copy link
Member

@swift-ci Please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants