Taken from:
http://www.scala-lang.org/api/current/scala/collection/immutable/Stream.html
def from(n: Int): Stream[Int] =
Stream.cons(n, from(n + 1))
// Here's the loop...
def sieve(s: Stream[Int]): Stream[Int] =
Stream.cons(s.head, sieve(s.tail filter { _ % s.head != 0 }))
def primes = sieve(from(2))
scala> primes take 10 print
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, empty
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.