Skip to content

Add joined(by:)#138

Merged
natecook1000 merged 4 commits into
apple:mainfrom
timvermeulen:joined-by
Jun 1, 2021
Merged

Add joined(by:)#138
natecook1000 merged 4 commits into
apple:mainfrom
timvermeulen:joined-by

Conversation

@timvermeulen
Copy link
Copy Markdown
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
Copy Markdown

@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!

Comment thread Sources/Algorithms/Intersperse.swift Outdated
}

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

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 {

Comment thread Sources/Algorithms/Intersperse.swift Outdated
}

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

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? {

Comment thread Sources/Algorithms/Intersperse.swift Outdated
}

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

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 {

Comment thread Sources/Algorithms/EitherSequence.swift Outdated
}

extension EitherSequence: Sequence {
public struct Iterator: IteratorProtocol {
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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