Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.