Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Last active August 29, 2015 14:17
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 klaeufer/867abb57dddc1b247b52 to your computer and use it in GitHub Desktop.
Save klaeufer/867abb57dddc1b247b52 to your computer and use it in GitHub Desktop.
Scala Iterator loop idiom (stateless): repeatedly apply an endofunction until a condition is met
scala> def f(i: Int) = i * i
f: (i: Int)Int
scala> Iterator.iterate(2)(f)
res11: Iterator[Int] = non-empty iterator
scala> res11 takeWhile { _ < 10 } foreach { println }
2
4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment