Skip to content

Instantly share code, notes, and snippets.

@byF
Created June 29, 2020 11:13
Show Gist options
  • Save byF/89776f7d8ea6e3072a53091fb74d0bd7 to your computer and use it in GitHub Desktop.
Save byF/89776f7d8ea6e3072a53091fb74d0bd7 to your computer and use it in GitHub Desktop.
object LookBehindIterator {
trait LookBehindIterator[+A] extends Iterator[A] {
def last: A
def lastOption: Option[A] = Option(last)
}
implicit class ImplicitLookBehindIterator[T](val it: Iterator[T]) extends AnyVal {
def lookBehind: LookBehindIterator[T] =
new scala.collection.AbstractIterator[T] with LookBehindIterator[T] {
private[this] var lastHd: T = _
def last: T = lastHd
def hasNext: Boolean = it.hasNext
def next(): T = {
lastHd = it.next()
lastHd
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment