Created
December 9, 2011 02:43
-
-
Save pmatiello/1449901 to your computer and use it in GitHub Desktop.
Scala Streams
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
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