Skip to content

Instantly share code, notes, and snippets.

@filosganga
Created October 27, 2013 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filosganga/7187623 to your computer and use it in GitHub Desktop.
Save filosganga/7187623 to your computer and use it in GitHub Desktop.
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