Created
February 23, 2016 13:18
-
-
Save asflierl/a23186848d705302fe01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class RichTraversable[A, C[B] <: TraversableLike[B, C[B]]](val wrapped: C[A]) extends AnyVal { | |
def splitBy(p: A => Boolean)(implicit cb: CanBuild[Either[C[A], C[A]], C[Either[C[A], C[A]]]]): C[Either[C[A], C[A]]] = { | |
val builder = cb() | |
@tailrec def spanNext(as: C[A]): Unit = | |
if (as.nonEmpty) { | |
val ph = p(as.head) | |
val (append, remaining) = as.span(a => p(a) == ph) | |
builder += (if (ph) Right(append) else Left(append) : Either[C[A], C[A]]) | |
spanNext(remaining) | |
} | |
spanNext(wrapped) | |
builder.result() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment