Skip to content

Instantly share code, notes, and snippets.

@nuttycom
Created November 24, 2010 23:58
Show Gist options
  • Save nuttycom/714663 to your computer and use it in GitHub Desktop.
Save nuttycom/714663 to your computer and use it in GitHub Desktop.
What Iterator.iterate should have been.
object Bah {
def iterate[T](t: T)(f: T => Option[T]): Iterator[T] = new Iterator {
var next = Some(t)
override def hasNext = next.isDefined
override def next = {
val result = next.get
next = f(result)
result
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment