scala.collection.immutable

class Stream

[source: scala/collection/immutable/Stream.scala]

abstract class Stream[+A]
extends LinearSequence[A] with TraversableClass[A, Stream] with LinearSequenceTemplate[A, Stream[A]]

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

 object Main extends 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 { _ % s.head != 0 }))

   def primes = sieve(from(2))

   primes take 10 print
 }
 
Author
Martin Odersky, Matthias Zenger
Version
1.1 08/08/03
Direct Known Subclasses:
Stream.Empty, Stream.Cons

Method Summary
override def ++ [B >: A, That](that : Traversable[B])(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Create a new stream which contains all elements of this stream followed by all elements of Traversable `that'
override def ++ [B >: A, That](that : Iterator[B])(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Create a new stream which contains all elements of this stream followed by all elements of Iterator `that'
override def addString (b : StringBuilder, start : java.lang.String, sep : java.lang.String, end : java.lang.String) : StringBuilder
Write all defined elements of this iterable into given string builder. The written text begins with the string start and is finished by the string end. Inside, the string representations of defined elements (w.r.t. the method toString()) are separated by the string sep. The method will not force evaluation of undefined elements. A tail of such elements will be represented by a "?" instead.
def append [B >: A](rest : => Traversable[B]) : Stream[B]
The stream resulting from the concatenation of this stream with the argument stream.
override def companion : Companion[Stream]
The factory companion object that builds instances of class CC
override def dropWhile (p : (A) => Boolean) : Stream[A]
Returns the longest suffix of this iterable whose first element does not satisfy the predicate p.
override def filter (p : (A) => Boolean) : Stream[A]
Returns all the elements of this stream that satisfy the predicate p. The order of the elements is preserved.
override def flatMap [B, That](f : (A) => Traversable[B])(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Applies the given function f to each element of this stream, then concatenates the results.
def force : Stream[A]
Force evaluation of the whole stream and return it
override def hasDefiniteSize : Boolean
Returns true if this collection is known to have finite size. This is the case if the collection type is strict, or if the collection type is non-strict (e.g. it's a Stream), but all collection elements have been computed. Many methods in this trait will not work on collections of infinite sizes.
abstract def head : A
The first element of this stream
override def init : Stream[A]
The stream without its last element.
abstract def isEmpty : Boolean
is this stream empty?
override def map [B, That](f : (A) => B)(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Returns the stream resulting from applying the given function f to each element of this stream.
override def padTo [B >: A, That](len : Int, elem : B)(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Returns a new sequence of given length containing the elements of this sequence followed by zero or more occurrences of given elements.
override def partition (p : (A) => Boolean) : (Stream[A], Stream[A])
Returns all the elements of this stream that satisfy the predicate p. The order of the elements is preserved.
def print : Unit
Prints elements of this stream one by one, separated by commas
def print (sep : java.lang.String) : Unit
Prints elements of this stream one by one, separated by sep
override def removeDuplicates : Stream[A]
Builds a new stream from this stream in which any duplicates (wrt to ==) removed. Among duplicate elements, only the first one is retained in the result stream
override def reverse : Stream[A]
A list consisting of all elements of this list in reverse order.
override def slice (start : Int, end : Int) : Stream[A]
A substream starting at index `from` and extending up to (but not including) index `until`.
override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation as ``Stream''.
abstract def tail : Stream[A]
A stream consisting of the remaining elements of this stream after the first one.
protected abstract def tailDefined : Boolean
Is the tail of this stream defined?
override def take (n : Int) : Stream[A]
Returns the n first elements of this stream, or else the whole stream, if it has less than n elements.
override def takeRight (n : Int) : Stream[A]
Returns the rightmost n elements from this iterable.
override def takeWhile (p : (A) => Boolean) : Stream[A]
Returns the longest prefix of this stream whose elements satisfy the predicate p.
override def toStream : Stream[A]
Returns a stream with all elements in this traversable object.
override def zip [A1 >: A, B, That](that : Sequence[B])(implicit bf : BuilderFactory[(A1, B), That, Stream[A]]) : That
Returns a stream formed from this stream and the specified stream that by associating each element of the former with the element at the same position in the latter. If one of the two streams is longer than the other, its remaining elements are ignored.
override def zipWithIndex [A1 >: A, That](implicit bf : BuilderFactory[(A1, Int), That, Stream[A]]) : That
Zips this iterable with its indices. `s.zipWithIndex` is equivalent to `s zip s.indices`
Methods inherited from LinearSequenceTemplate
length, apply, iterator, foreach, forall, exists, count, find, foldLeft, foldRight, reduceLeft, reduceRight, last, drop, dropRight, span, sameElements, lengthCompare, isDefinedAt, segmentLength, indexWhere, lastIndexWhere, equals
Methods inherited from Sequence
hashCode
Methods inherited from SequenceTemplate
size, zipAll, prefixLength, indexWhere, findIndexOf, indexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexWhere, reverseIterator, reversedElements, startsWith, startsWith, endsWith, indexOfSeq, indexOfSeq, lastIndexOfSeq, lastIndexOfSeq, contains, union, diff, intersect, patch, toSequence, indices, view, view, toString, sortWith, findLastIndexOf, slice, equalsWith, containsSlice, projection
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from IterableTemplate
elements, toIterable, first, firstOption, toSeq
Methods inherited from TraversableClass
newBuilder, genericBuilder, unzip, flatten, transpose
Methods inherited from TraversableTemplate
thisCollection, nonEmpty, filterMap, filterNot, remove, groupBy, /:, :\, reduceLeftOption, reduceRightOption, headOption, lastOption, splitAt, copyToBuffer, copyToArray, copyToArray, toArray, toList, toSet, mkString, mkString, mkString, addString, addString
Methods inherited from AnyRef
getClass, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
override def companion : Companion[Stream]
The factory companion object that builds instances of class CC
Overrides
LinearSequence.companion, TraversableClass.companion

abstract def isEmpty : Boolean
is this stream empty?
Overrides
TraversableClass.isEmpty, LinearSequenceTemplate.isEmpty

abstract def head : A
The first element of this stream
Throws
Predef.NoSuchElementException - if the stream is empty.
Overrides
TraversableClass.head, LinearSequenceTemplate.head

abstract def tail : Stream[A]
A stream consisting of the remaining elements of this stream after the first one.
Throws
Predef.UnsupportedOperationException - if the stream is empty.
Overrides
LinearSequenceTemplate.tail

protected abstract def tailDefined : Boolean
Is the tail of this stream defined?

def append[B >: A](rest : => Traversable[B]) : Stream[B]
The stream resulting from the concatenation of this stream with the argument stream.
Parameters
rest - The stream that gets appended to this stream

def force : Stream[A]
Force evaluation of the whole stream and return it

def print : Unit
Prints elements of this stream one by one, separated by commas

def print(sep : java.lang.String) : Unit
Prints elements of this stream one by one, separated by sep
Parameters
sep - The separator string printed between consecutive elements.

override def toStream : Stream[A]
Returns a stream with all elements in this traversable object.

override def hasDefiniteSize : Boolean
Returns true if this collection is known to have finite size. This is the case if the collection type is strict, or if the collection type is non-strict (e.g. it's a Stream), but all collection elements have been computed. Many methods in this trait will not work on collections of infinite sizes.

override def ++[B >: A, That](that : Traversable[B])(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Create a new stream which contains all elements of this stream followed by all elements of Traversable `that'
Notes
It's subtle why this works. We know that if the target type of the Builder That is either a Stream, or one of its supertypes, or undefined, then StreamBuilder will be chosen for the implicit. we recognize that fact and optimize to get more laziness.

override def ++[B >: A, That](that : Iterator[B])(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Create a new stream which contains all elements of this stream followed by all elements of Iterator `that'

override def map[B, That](f : (A) => B)(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Returns the stream resulting from applying the given function f to each element of this stream.
Parameters
f - function to apply to each element.
Returns
f(a0), ..., f(an) if this sequence is a0, ..., an.

override def flatMap[B, That](f : (A) => Traversable[B])(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Applies the given function f to each element of this stream, then concatenates the results.
Parameters
f - the function to apply on each element.
Returns
f(a0) ::: ... ::: f(an) if this stream is [a0, ..., an].

override def filter(p : (A) => Boolean) : Stream[A]
Returns all the elements of this stream that satisfy the predicate p. The order of the elements is preserved.
Parameters
p - the predicate used to filter the stream.
Returns
the elements of this stream satisfying p.

override def partition(p : (A) => Boolean) : (Stream[A], Stream[A])
Returns all the elements of this stream that satisfy the predicate p. The order of the elements is preserved.
Parameters
p - the predicate used to filter the stream.
Returns
the elements of this stream satisfying p.

override def zip[A1 >: A, B, That](that : Sequence[B])(implicit bf : BuilderFactory[(A1, B), That, Stream[A]]) : That
Returns a stream formed from this stream and the specified stream that by associating each element of the former with the element at the same position in the latter. If one of the two streams is longer than the other, its remaining elements are ignored.
Returns
Stream({a0,b0}, ..., {amin(m,n),bmin(m,n))} when Stream(a0, ..., am) zip Stream(b0, ..., bn) is invoked.

override def zipWithIndex[A1 >: A, That](implicit bf : BuilderFactory[(A1, Int), That, Stream[A]]) : That
Zips this iterable with its indices. `s.zipWithIndex` is equivalent to `s zip s.indices`

override def addString(b : StringBuilder, start : java.lang.String, sep : java.lang.String, end : java.lang.String) : StringBuilder
Write all defined elements of this iterable into given string builder. The written text begins with the string start and is finished by the string end. Inside, the string representations of defined elements (w.r.t. the method toString()) are separated by the string sep. The method will not force evaluation of undefined elements. A tail of such elements will be represented by a "?" instead.

override def take(n : Int) : Stream[A]
Returns the n first elements of this stream, or else the whole stream, if it has less than n elements.
Parameters
n - the number of elements to take.
Returns
the n first elements of this stream.
Overrides
LinearSequenceTemplate.take

override def slice(start : Int, end : Int) : Stream[A]
A substream starting at index `from` and extending up to (but not including) index `until`.
Notes
This is equivalent to (but possibly more efficient than) c.drop(from).take(to - from)
Might return different results for different runs, unless this iterable is ordered
Parameters
start - The index of the first element of the returned subsequence
end - The index of the element following the returned subsequence
Throws
IndexOutOfBoundsException - if from < 0 or length < from + len

override def init : Stream[A]
The stream without its last element.
Throws
Predef.UnsupportedOperationException - if the stream is empty.

override def takeRight(n : Int) : Stream[A]
Returns the rightmost n elements from this iterable.
Parameters
n - the number of elements to take

override def takeWhile(p : (A) => Boolean) : Stream[A]
Returns the longest prefix of this stream whose elements satisfy the predicate p.
Parameters
p - the test predicate.

override def dropWhile(p : (A) => Boolean) : Stream[A]
Returns the longest suffix of this iterable whose first element does not satisfy the predicate p.
Parameters
p - the test predicate.

override def removeDuplicates : Stream[A]
Builds a new stream from this stream in which any duplicates (wrt to ==) removed. Among duplicate elements, only the first one is retained in the result stream

override def padTo[B >: A, That](len : Int, elem : B)(implicit bf : BuilderFactory[B, That, Stream[A]]) : That
Returns a new sequence of given length containing the elements of this sequence followed by zero or more occurrences of given elements.

override def reverse : Stream[A]
A list consisting of all elements of this list in reverse order.

override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation as ``Stream''.