Created
June 29, 2020 11:13
-
-
Save byF/89776f7d8ea6e3072a53091fb74d0bd7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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