scala.collection.generic

trait IterableTemplate

[source: scala/collection/generic/IterableTemplate.scala]

trait IterableTemplate[+A, +This <: IterableTemplate[A, This] with Iterable[A]]
extends TraversableTemplate[A, This]

A template trait for iterable collections.

Collection classes mixing in this trait provide a method iterator which returns an iterator over all the elements contained in the collection. They also provide a method newBuilder which creates a builder for collections of the same kind.

This trait implements Traversable's foreach method by stepping through all elements. Subclasses of Iterable should re-implement foreach with something more efficient, if possible.

This trait adds methods iterator, sameElements, takeRight, dropRight to the methods inherited from trait Traversable.

Author
Martin Odersky
Version
2.8
Direct Known Subclasses:
Iterable, IterableProxyTemplate, IterableViewTemplate, MapTemplate, SequenceTemplate, SetTemplate, Iterable, Iterable

Method Summary
def dropRight (n : Int) : This
Returns the iterable wihtout its rightmost n elements.
def elements : Iterator[A]
def first : A
def firstOption : Option[A]
None if traversable is empty.
override def foldRight [B](z : B)(op : (A, B) => B) : B
Combines the elements of this iterable together using the binary function f, from right to left, and starting with the value z.
def foreach [U](f : (A) => U) : Unit
Apply a function f to all elements of this traversable object.
override def head : A
The first element of this iterable.
override def isEmpty : Boolean
Does this iterable contain no elements?
abstract def iterator : Iterator[A]
Creates a new iterator over all elements contained in this iterable object.
def projection : IterableView[A, This]
returns a projection that can be used to call non-strict filter, map, and flatMap methods that build projections of the collection.
override def reduceRight [B >: A](op : (A, B) => B) : B
Combines the elements of this iterable object together using the binary operator op, from right to left
def sameElements [B >: A](that : Iterable[B]) : Boolean
Checks if the other iterable object contains the same elements as this one.
def takeRight (n : Int) : This
Returns the rightmost n elements from this iterable.
override def toIterable : Iterable[A]
The iterable itself
def toSeq : Sequence[A]
override def toStream : Stream[A]
Returns a stream with all elements in this traversable object.
override def view : IterableView[A, This]
Creates a view of this iterable @see IterableView
override def view (from : Int, until : Int) : IterableView[A, This]
A sub-iterable view starting at index `from` and extending up to (but not including) index `until`.
Methods inherited from TraversableTemplate
newBuilder (abstract), thisCollection, nonEmpty, size, hasDefiniteSize, ++, ++, map, flatMap, filter, filterMap, filterNot, remove, partition, groupBy, forall, exists, count, find, foldLeft, /:, :\, reduceLeft, reduceLeftOption, reduceRightOption, headOption, tail, last, lastOption, init, take, drop, slice, takeWhile, dropWhile, span, splitAt, copyToBuffer, copyToArray, copyToArray, toArray, toList, toSequence, toSet, mkString, mkString, mkString, addString, addString, addString, toString, stringPrefix
Methods inherited from AnyRef
getClass, hashCode, equals, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
abstract def iterator : Iterator[A]
Creates a new iterator over all elements contained in this iterable object.
Returns
the new iterator

@deprecated("use `iterator' instead")

def elements : Iterator[A]

def foreach[U](f : (A) => U) : Unit
Apply a function f to all elements of this traversable object.
Parameters
f - A function that is applied for its side-effect to every element. The result (of arbitrary type U) of function `f` is discarded.
Notes
This method underlies the implementation of most other bulk operations. Implementing `foreach` with `iterator` is often suboptimal. So `foreach` should be overridden in concrete collection classes if a more efficient implementation is available.
Overrides
TraversableTemplate.foreach

override def isEmpty : Boolean
Does this iterable contain no elements?
Overrides
TraversableTemplate.isEmpty

override def foldRight[B](z : B)(op : (A, B) => B) : B
Combines the elements of this iterable together using the binary function f, from right to left, and starting with the value z.
Notes
Will not terminate for infinite-sized collections.
Might return different results for different runs, unless this iterable is ordered, or the operator is associative and commutative.
Returns
f(a0, f(a1, f(..., f(an, z)...))) if the iterable is [a0, a1, ..., an].
Overrides
TraversableTemplate.foldRight

override def reduceRight[B >: A](op : (A, B) => B) : B
Combines the elements of this iterable object together using the binary operator op, from right to left
Notes
Will not terminate for infinite-sized collections.
Might return different results for different runs, unless this iterable is ordered, or the operator is associative and commutative.
Parameters
op - The operator to apply
Returns
a0 op (... op (an-1 op an)...) if the iterable object has elements a0, a1, ..., an.
Throws
Predef.UnsupportedOperationException - if the iterator is empty.
Overrides
TraversableTemplate.reduceRight

override def toIterable : Iterable[A]
The iterable itself
Overrides
TraversableTemplate.toIterable

override def head : A
The first element of this iterable.
Notes
Might return different results for different runs, unless this iterable is ordered
Throws
Predef.NoSuchElementException - if the iterable is empty.
Overrides
TraversableTemplate.head

def takeRight(n : Int) : This
Returns the rightmost n elements from this iterable.
Parameters
n - the number of elements to take
Notes
Might return different results for different runs, unless this iterable is ordered

def dropRight(n : Int) : This
Returns the iterable wihtout its rightmost n elements.
Parameters
n - the number of elements to take
Notes
Might return different results for different runs, unless this iterable is ordered

def sameElements[B >: A](that : Iterable[B]) : Boolean
Checks if the other iterable object contains the same elements as this one.
Notes
will not terminate for infinite-sized iterables.
Might return different results for different runs, unless this iterable is ordered
Parameters
that - the other iterable
Returns
true, iff both iterables contain the same elements.

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

override def view : IterableView[A, This]
Creates a view of this iterable @see IterableView
Overrides
TraversableTemplate.view

override def view(from : Int, until : Int) : IterableView[A, This]
A sub-iterable view starting at index `from` and extending up to (but not including) index `until`.
Parameters
from - The index of the first element of the slice
until - The index of the element following the slice
Notes
The difference between `view` and `slice` is that `view` produces a view of the current iterable, whereas `slice` produces a new iterable.
Might return different results for different runs, unless this iterable is ordered
view(from, to) is equivalent to view.slice(from, to)
Overrides
TraversableTemplate.view

@deprecated("use `head' instead")

def first : A

@deprecated("use `headOption' instead")

def firstOption : Option[A]
None if traversable is empty.

@deprecated("use `toSequence' instead")

def toSeq : Sequence[A]

@deprecated("use `view' instead")

def projection : IterableView[A, This]
returns a projection that can be used to call non-strict filter, map, and flatMap methods that build projections of the collection.