Skip to content

Instantly share code, notes, and snippets.

@hussachai
Last active November 15, 2015 04:10
Show Gist options
  • Save hussachai/6eaf6b12527c61ba2ac5 to your computer and use it in GitHub Desktop.
Save hussachai/6eaf6b12527c61ba2ac5 to your computer and use it in GitHub Desktop.
def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b)
val fib1 = fibFrom(0, 1) //0 1 1 2 3 5 8 …
val fib5 = fibFrom(0, 5) //0 5 5 10 15 …
//fib1.force //Don’t do this cause it will call the function infinitely and soon you will get the OutOfMemoryError
//fib1.size //Don’t do this too with the same reason as above.
fib1.take(10) //Do this. It will take the first 10 from the inifite Stream.
fib1.take(20).foreach(println(_)) //Prints 20 first numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment