Question on usage of Stream

Hello,

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?
 
Thanks,
Sébastien

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