swift-algorithms icon indicating copy to clipboard operation
swift-algorithms copied to clipboard

Split a collection using a collection separator

Open timvermeulen opened this issue 4 years ago • 0 comments

Depends on #154.

Note that the overload on LazyCollectionProtocol returns a LazyCollection<SplitCollection<Elements, Separator>> rather than a SplitCollection<Self, Separator>. The purpose of this is to have it return slices of the underlying collection, rather than slices of the lazy wrapper.

We'll also need to unify the name of the type added here (currently SplitCollection) with the type returned by the lazy split operation that takes a single element separator (currently LazySplitCollection).

extension Collection {
  public func split<Separator: Collection>(
    separator: Separator,
    maxSplits: Int = Int.max,
    omittingEmptySubsequences: Bool = true,
    by areEquivalent: (Element, Separator.Element) throws -> Bool
  ) rethrows -> [SubSequence]
}

extension Collection where Element: Equatable {
  public func split<Separator: Collection>(
    separator: Separator,
    maxSplits: Int = Int.max,
    omittingEmptySubsequences: Bool = true
  ) -> SplitCollection<Self, Separator>
    where Separator.Element == Element
}

extension LazyCollectionProtocol {
  public func split<Separator: Collection>(
    separator: Separator,
    maxSplits: Int = Int.max,
    omittingEmptySubsequences: Bool = true,
    by areEquivalent: @escaping (Element, Separator.Element) -> Bool
  ) -> LazyCollection<SplitCollection<Elements, Separator>>
}

extension LazyCollectionProtocol where Element: Equatable {
  public func split<Separator: Collection>(
    separator: Separator,
    maxSplits: Int = Int.max,
    omittingEmptySubsequences: Bool = true
  ) -> LazyCollection<SplitCollection<Elements, Separator>>
    where Separator.Element == Element
}

Checklist

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

timvermeulen avatar Jul 23 '21 15:07 timvermeulen