Skip to content

Instantly share code, notes, and snippets.

@pmatiello
Created December 9, 2011 02:43
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 pmatiello/1449901 to your computer and use it in GitHub Desktop.
Save pmatiello/1449901 to your computer and use it in GitHub Desktop.
Scala Streams
object Streams {
def main(args: Array[String]) {
val list = fibonacci()
(list take 20) foreach println
}
private def eagerNaturals() = {
def naturals(n: Int): List[Int] = n :: naturals(n + 1)
naturals(0)
}
private def naturals1() = {
def naturals(n: Int): Stream[Int] = n #:: naturals(n + 1)
naturals(0)
}
private def naturals2() = {
lazy val naturals: Stream[Int] = 0 #:: naturals.map(_ + 1)
naturals
}
private def fibonacci() = {
lazy val fibonacci: Stream[Int] = 1 #:: 1 #::
(fibonacci zip fibonacci.tail).map { case (a, b) => a + b }
fibonacci
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment