Skip to content

Instantly share code, notes, and snippets.

@ahoy-jon
Created January 24, 2018 16:34
Show Gist options
  • Save ahoy-jon/46bd7eab3f6df7a9bc6215fd662af92f to your computer and use it in GitHub Desktop.
Save ahoy-jon/46bd7eab3f6df7a9bc6215fd662af92f to your computer and use it in GitHub Desktop.
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