Created
October 27, 2013 20:40
-
-
Save filosganga/7187623 to your computer and use it in GitHub Desktop.
An Iterator that cycles trough the given elements
This file contains 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
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