Created
January 24, 2018 16:34
-
-
Save ahoy-jon/46bd7eab3f6df7a9bc6215fd662af92f 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
object Multiline { | |
def splitOn[T](iterator: Iterator[T])(split: T => Boolean): Iterator[Seq[T]] = { | |
new Iterator[Seq[T]] { | |
var nextFrame: Seq[T] = Vector.empty | |
override def hasNext: Boolean = nextFrame.nonEmpty || iterator.hasNext | |
override def next(): Seq[T] = { | |
while (iterator.hasNext) { | |
val t = iterator.next() | |
if (nextFrame.isEmpty) { | |
nextFrame = nextFrame :+ t | |
} else { | |
if (split(t)) { | |
val res = nextFrame | |
nextFrame = Vector(t) | |
return res | |
} else { | |
nextFrame = nextFrame :+ t | |
} | |
} | |
} | |
val res = nextFrame | |
nextFrame = Vector() | |
res | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment