in scala
trait Stream

trait Stream[a]()
extends Seq[a]
with ScalaObject

The class Stream implements lazy lists where elements are only evaluated when they are needed. Here is an example:

 object Main with Application {

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

   def sieve(s: Stream[Int]): Stream[Int] =
     Stream.cons(s.head, sieve(s.tail filter { x => x % s.head != 0 }));

   def primes = sieve(from(2));

   primes take 10 print
 }
 
Author:
Martin Odersky, Matthias Zenger
Version:
1.1 08/08/03

Method Summary
  def append[b >: a](def rest: Stream[b]): Stream[b]
  def apply(n: Int): a
  def at(n: Int): a
override def copyToArray[b >: a](xs: Array[b], start: Int): Array[b]
     Fills the given array xs with the elements of this sequence starting at position start.
override def drop(n: Int): Stream[a]
     Returns a new sub-sequence that drops the first n elements of this sequence.
  def dropWhile(p: (a) => Boolean): Stream[a]
  def elements: Iterator[a]
     Creates a new iterator over all elements contained in this object.
override def exists(p: (a) => Boolean): Boolean
     Apply a predicate p to all elements of this iterable object and return true, iff there is at least one element for which p yields true.
  def filter(p: (a) => Boolean): Stream[a]
  def flatMap[b](f: (a) => Stream[b]): Stream[b]
override def foldLeft[b](z: b)(f: (b,a) => b): b
     Combines the elements of this list together using the binary operator op, from left to right, and starting with the value z.
override def foldRight[b](z: b)(f: (a,b) => b): b
     Combines the elements of this list together using the binary operator op, from rigth to left, and starting with the value z.
override def forall(p: (a) => Boolean): Boolean
     Apply a predicate p to all elements of this iterable object and return true, iff the predicate yields true for all elements.
override def foreach(f: (a) => Unit): Unit
     Apply a function f to all elements of this iterable object.
abstract def head: a
  def init: Stream[a]
abstract def isEmpty: Boolean
  def last: a
  def length: Int
     Returns the length of the sequence.
  def map[b](f: (a) => b): Stream[b]
  def print: Unit
abstract def printElems(buf: StringBuffer, prefix: String): StringBuffer
  def reduceLeft[b >: a](f: (b,b) => b): b
  def reduceRight[b >: a](f: (b,b) => b): b
  def reverse: Stream[a]
abstract def tail: Stream[a]
override def take(n: Int): Stream[a]
     Returns the sub-sequence starting from index n.
  def takeWhile(p: (a) => Boolean): Stream[a]
override def toString(): String
     Customizes the toString method.
  def zip[b](that: Stream[b]): Stream[Tuple2[a,b]]

Methods inherited from java/lang/Object-class
clone, eq, equals, finalize, getClass, hashCode, notify, notifyAll, synchronized, wait, wait, wait

Methods inherited from scala/Any-class
!=, ==, asInstanceOf, isInstanceOf, match

Methods inherited from scala/Iterable-class
/:, :\, find, sameElements

Methods inherited from scala/Seq-class
indexOf, isDefinedAt, lastIndexOf, stringPrefix, subseq, toList

Method Detail

isEmpty

  abstract def isEmpty: Boolean

head

  abstract def head: a

tail

  abstract def tail: Stream[a]

length

  def length: Int
Returns the length of the sequence.
Returns:
the sequence length.

append

  def append[b >: a](def rest: Stream[b]): Stream[b]

elements

  def elements: Iterator[a]
Creates a new iterator over all elements contained in this object.
Returns:
the new iterator

init

  def init: Stream[a]

last

  def last: a

take

  override def take(n: Int): Stream[a]
Returns the sub-sequence starting from index n.

drop

  override def drop(n: Int): Stream[a]
Returns a new sub-sequence that drops the first n elements of this sequence.

apply

  def apply(n: Int): a

at

  def at(n: Int): a

takeWhile

  def takeWhile(p: (a) => Boolean): Stream[a]

dropWhile

  def dropWhile(p: (a) => Boolean): Stream[a]

map

  def map[b](f: (a) => b): Stream[b]

foreach

  override def foreach(f: (a) => Unit): Unit
Apply a function f to all elements of this iterable object.
Parameters:
f - a function that is applied to every element.

filter

  def filter(p: (a) => Boolean): Stream[a]

forall

  override def forall(p: (a) => Boolean): Boolean
Apply a predicate p to all elements of this iterable object and return true, iff the predicate yields true for all elements.
Parameters:
p - the predicate
Returns:
true, iff the predicate yields true for all elements.

exists

  override def exists(p: (a) => Boolean): Boolean
Apply a predicate p to all elements of this iterable object and return true, iff there is at least one element for which p yields true.
Parameters:
p - the predicate
Returns:
true, iff the predicate yields true for at least one element.

foldLeft

  override def foldLeft[b](z: b)(f: (b,a) => b): b
Combines the elements of this list together using the binary operator op, from left to right, and starting with the value z.
Returns:
op(... (op(op(z,a0),a1) ...), an) if the list is List(a0, a1, ..., an).

foldRight

  override def foldRight[b](z: b)(f: (a,b) => b): b
Combines the elements of this list together using the binary operator op, from rigth to left, and starting with the value z.
Returns:
a0 op (... op (an op z)...) if the list is [a0, a1, ..., an].

reduceLeft

  def reduceLeft[b >: a](f: (b,b) => b): b

reduceRight

  def reduceRight[b >: a](f: (b,b) => b): b

flatMap

  def flatMap[b](f: (a) => Stream[b]): Stream[b]

reverse

  def reverse: Stream[a]

copyToArray

  override def copyToArray[b >: a](xs: Array[b], start: Int): Array[b]
Fills the given array xs with the elements of this sequence starting at position start.
Parameters:
xs - the array to fill.
start - starting index.
Returns:
the given array xs filled with this list.

zip

  def zip[b](that: Stream[b]): Stream[Tuple2[a,b]]

print

  def print: Unit

toString

  override def toString(): String
Customizes the toString method.
Returns:
a string representation of this sequence.

printElems

  abstract def printElems(buf: StringBuffer, prefix: String): StringBuffer