Skip to content

Import splitWith from strawman-contrib #6

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/scala/scala/collection/decorators/SeqDecorator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,48 @@ class SeqDecorator[C, S <: HasSeqOps[C]](coll: C)(implicit val seq: S) {
def intersperse[B >: seq.A, That](start: B, sep: B, end: B)(implicit bf: BuildFrom[C, B, That]): That =
bf.fromSpecificIterable(coll)(new View.IntersperseSurround(seq(coll), start, sep, end))

/** Splits this collection into groups according to the given predicate.
*
* @param p the predicate used to discriminate elements
* @return A nested collection with groups of elements,
* opening new groups whenever the predicate
* changes the return type
*
* @example {{{
* // Example 1: Split a list of integers into groups that are even / odd
* List(1, 2, 4, 6, 7).splitWith(i => i % 2 == 0) => List(List(1), List(2, 4, 6), List(7))
*
* // Example 2: Split a list of chars into groups that are upper case or lower case
* List('a', 'b', 'C', 'D', 'e', 'f').splitWith(_.isUpper) => List(List('a', 'b'), List('C', 'D'), List('e', 'f'))
* }}}
*/
def splitWith[Group, That](p: seq.A => Boolean)(implicit bfGroup: BuildFrom[C, seq.A, Group], bfThat: BuildFrom[C, Group, That]): That = {
def newGroupBuilder() = bfGroup.newBuilder(coll)

val groups: mutable.Builder[Group, That] = bfThat.newBuilder(coll)
val it: Iterator[seq.A] = seq(coll).iterator

var currentGroup = newGroupBuilder()
var lastTestResult = Option.empty[Boolean]

while (it.hasNext) {
val elem = it.next()
val currentTest = p(elem)

lastTestResult match {
case None =>
currentGroup.addOne(elem)
case Some(lastTest) if currentTest == lastTest =>
currentGroup.addOne(elem)
case Some(_) =>
groups.addOne(currentGroup.result())
currentGroup = newGroupBuilder().addOne(elem)
}

lastTestResult = Some(currentTest)
}

groups.addOne(currentGroup.result()).result()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class SeqDecoratorTest {
assertEquals(List(0, 1, 2), List(1).intersperse(0, 5, 2))
}

@Test def splitWith(): Unit = {
assertEquals(List(List()), List().splitWith(_ => true))
assertEquals(List(List()), List().splitWith(_ => false))
assertEquals(List(List(1), List(2, 4, 6), List(7)), List(1, 2, 4, 6, 7).splitWith(i => i % 2 == 0))
assertEquals(List(List('a', 'b'), List('C', 'D'), List('e', 'f')), List('a', 'b', 'C', 'D', 'e', 'f').splitWith(_.isUpper))
}

// This test just checks that there is no compilation error
@Test def genericDecorator(): Unit = {
val vector = Vector(1, 2, 3)
Expand Down