Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Iterator that cycles trough the given elements
class CycleIterator[B <: A, +A](elements: Iterable[B]) extends Iterator[A] {
var iterator: Iterator[B] = Iterator.empty
def hasNext: Boolean = {
if (!iterator.hasNext) {
iterator = elements.iterator
}
iterator.hasNext
}
def next(): A = iterator.next()
}
object CycleIterator {
def apply[B <: A, A](xs: Iterable[B]): CycleIterator[B, A] = new CycleIterator[B, A](xs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment