This page is no longer maintained — Please continue to the home page at

Question on usage of Stream

No replies
Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.

The following simple example illustrates the usage of streams in the documentation of Stream.scala:

> def from(n: Int): Stream[Int] = Stream.cons(n, from(n + 1))

It looks very simple but the problem is that obviously the method is not tail recursive and if I do:

> from(2) take 3715 print

I get a java.lang.StackOverflowError

This shows that building infinite streams this way is quite limited :)

But is there a better way?

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland