Scala Library
|
|
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 }
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 |
abstract
def
isEmpty : Boolean
abstract
def
head : A
protected abstract
def
tailDefined : Boolean
def
append[B >: A](rest : => Traversable[B]) : Stream[B]
rest -
The stream that gets appended to this stream
def
print : Unit
def
print(sep : java.lang.String) : Unit
sep
sep -
The separator string printed between consecutive elements.override
def
hasDefiniteSize : Boolean
override
def
++[B >: A, That](that : Traversable[B])(implicit
bf : BuilderFactory[B, That, Stream[A]]) : That
override
def
++[B >: A, That](that : Iterator[B])(implicit
bf : BuilderFactory[B, That, Stream[A]]) : That
f
to each element of this stream.f -
function to apply to each element.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
f
to each element of
this stream, then concatenates the results.f -
the function to apply on each element.f(a0) ::: ... ::: f(an)
if this stream is [a0, ..., an]
.p
. The order of the elements is preserved.p -
the predicate used to filter the stream.p
.p
. The order of the elements is preserved.p -
the predicate used to filter the stream.p
.override
def
zip[A1 >: A, B, That](that : Sequence[B])(implicit
bf : BuilderFactory[(A1, B), That, Stream[A]]) : That
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.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
override
def
addString(b : StringBuilder, start : java.lang.String, sep : java.lang.String, end : java.lang.String) : StringBuilder
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.n
first elements of this stream, or else the whole
stream, if it has less than n
elements.n -
the number of elements to take.n
first elements of this stream.start -
The index of the first element of the returned subsequenceend -
The index of the element following the returned subsequenceIndexOutOfBoundsException -
if from < 0
or length < from + len
n
elements from this iterable.n -
the number of elements to takep
.p -
the test predicate.p
.p -
the test predicate.override
def
padTo[B >: A, That](len : Int, elem : B)(implicit
bf : BuilderFactory[B, That, Stream[A]]) : That
override
def
stringPrefix : java.lang.String
toString
representation as ``Stream''.
Scala Library
|
|