scala.collection

trait IterableLike

[source: scala/collection/IterableLike.scala]

trait IterableLike[+A, +Repr]
extends Equals with TraversableLike[A, Repr]

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 Iterable'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 Iterable.

Notes
This trait replaces every method that uses breaks in the original by an iterator version.
Author
Martin Odersky
Version
2.8
Since
2.8
Direct Known Subclasses:
Iterable, IterableProxyLike, IterableViewLike, MapLike, SeqLike, SetLike, Iterable, Iterable

Method Summary
override def canEqual (that : Any) : Boolean
Method called from equality methods, so that user-defined subclasses can refuse to be equal to other collections of the same kind.
override def copyToArray [B >: A](xs : Array[B], start : Int, len : Int) : Unit
Fills the given array xs with at most `len` elements of this iterable starting at position `start`. Copying will stop once either the end of the current iterable is reached or `len` elements have been copied or the end of the array is reached.
def dropRight (n : Int) : Repr
Returns the iterable wihtout its rightmost n elements.
def elements : Iterator[A]
override def exists (p : (A) => Boolean) : Boolean
Return true iff there is an element in this iterable for which the given predicate `p` yields true.
override def find (p : (A) => Boolean) : Option[A]
Find and return the first element of the iterable object satisfying a predicate, if any.
def first : A
def firstOption : Option[A]
None if iterable 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.
override def forall (p : (A) => Boolean) : Boolean
Return true iff the given predicate `p` yields true for all elements of this iterable.
def foreach [U](f : (A) => U) : Unit
Apply a function f to all elements of this iterable 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, Repr]
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.
override def slice (from : Int, until : Int) : Repr
A sub-iterable starting at index `from` and extending up to (but not including) index `until`.
override def take (n : Int) : Repr
Return an iterable consisting only of the first n elements of this iterable, or else the whole iterable, if it has less than n elements.
def takeRight (n : Int) : Repr
Returns the rightmost n elements from this iterable.
override def takeWhile (p : (A) => Boolean) : Repr
Returns the longest prefix of this iterable whose elements satisfy the predicate p.
protected[this] override def thisCollection : Iterable[A]
protected[this] override def toCollection (repr : Repr) : Iterable[A]
override def toIterable : Iterable[A]
The iterable itself
override def toStream : Stream[A]
Returns a stream with all elements in this iterable object.
override def view (from : Int, until : Int) : IterableView[A, Repr]
A sub-iterable view starting at index `from` and extending up to (but not including) index `until`.
override def view : IterableView[A, Repr]
Creates a view of this iterable @see IterableView
def zip [A1 >: A, B, That](that : Iterable[B])(implicit bf : CanBuildFrom[Repr, (A1, B), That]) : That
Returns an iterable formed from this iterable and another iterable by combining corresponding elements in pairs. If one of the two iterables is longer than the other, its remaining elements are ignored.
def zipAll [B, A1 >: A, That](that : Iterable[B], thisElem : A1, thatElem : B)(implicit bf : CanBuildFrom[Repr, (A1, B), That]) : That
Returns an iterable formed from this iterable and the specified iterable that by associating each element of the former with the element at the same position in the latter.
def zipWithIndex [A1 >: A, That](implicit bf : CanBuildFrom[Repr, (A1, Int), That]) : That
Zips this iterable with its indices (startiong from 0).
Methods inherited from TraversableLike
newBuilder (abstract), repr, nonEmpty, size, hasDefiniteSize, ++, ++, map, flatMap, filter, filterNot, partialMap, remove, partition, groupBy, count, foldLeft, /:, :\, reduceLeft, reduceLeftOption, reduceRightOption, sum, product, min, max, headOption, tail, last, lastOption, init, drop, dropWhile, span, splitAt, copyToBuffer, copyToArray, toArray, toList, toSeq, toIndexedSeq, toSet, mkString, mkString, mkString, addString, addString, addString, toString, stringPrefix, withFilter
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
protected[this] override def thisCollection : Iterable[A]
Overrides
TraversableLike.thisCollection

protected[this] override def toCollection(repr : Repr) : Iterable[A]
Overrides
TraversableLike.toCollection

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 iterable 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
TraversableLike.foreach

override def forall(p : (A) => Boolean) : Boolean
Return true iff the given predicate `p` yields true for all elements of this iterable.
Notes
May not terminate for infinite-sized collections.
Parameters
p - the predicate
Overrides
TraversableLike.forall

override def exists(p : (A) => Boolean) : Boolean
Return true iff there is an element in this iterable for which the given predicate `p` yields true.
Notes
May not terminate for infinite-sized collections.
Parameters
p - the predicate
Overrides
TraversableLike.exists

override def find(p : (A) => Boolean) : Option[A]
Find and return the first element of the iterable object satisfying a predicate, if any.
Notes
may not terminate for infinite-sized collections.
Might return different results for different runs, unless this iterable is ordered.
Parameters
p - the predicate
Returns
an option containing the first element in the iterable object satisfying p, or None if none exists.
Overrides
TraversableLike.find

override def isEmpty : Boolean
Does this iterable contain no elements?
Overrides
TraversableLike.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
TraversableLike.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
TraversableLike.reduceRight

override def toIterable : Iterable[A]
The iterable itself
Overrides
TraversableLike.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
TraversableLike.head

override def take(n : Int) : Repr
Return an iterable consisting only of the first n elements of this iterable, or else the whole iterable, if it has less than n elements.
Parameters
n - the number of elements to take
Notes
Might return different results for different runs, unless this iterable is ordered
Overrides
TraversableLike.take

override def slice(from : Int, until : Int) : Repr
A sub-iterable starting at index `from` and extending up to (but not including) index `until`.
Notes
c.slice(from, to) 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
from - The index of the first element of the returned subsequence
until - The index of the element following the returned subsequence
Overrides
TraversableLike.slice

override def takeWhile(p : (A) => Boolean) : Repr
Returns the longest prefix of this iterable whose elements satisfy the predicate p.
Parameters
p - the test predicate.
Notes
Might return different results for different runs, unless this iterable is ordered
Overrides
TraversableLike.takeWhile

def takeRight(n : Int) : Repr
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) : Repr
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

override def copyToArray[B >: A](xs : Array[B], start : Int, len : Int) : Unit
Fills the given array xs with at most `len` elements of this iterable starting at position `start`. Copying will stop once either the end of the current iterable is reached or `len` elements have been copied or the end of the array is reached.
Notes
Will not terminate for infinite-sized collections.
Parameters
xs - the array to fill.
start - starting index.
len - number of elements to copy
Overrides
TraversableLike.copyToArray

def zip[A1 >: A, B, That](that : Iterable[B])(implicit bf : CanBuildFrom[Repr, (A1, B), That]) : That
Returns an iterable formed from this iterable and another iterable by combining corresponding elements in pairs. If one of the two iterables is longer than the other, its remaining elements are ignored.
Parameters
that - The iterable providing the second half of each result pair

def zipAll[B, A1 >: A, That](that : Iterable[B], thisElem : A1, thatElem : B)(implicit bf : CanBuildFrom[Repr, (A1, B), That]) : That
Returns an iterable formed from this iterable and the specified iterable that by associating each element of the former with the element at the same position in the latter.
Parameters
that - iterable that may have a different length as the self iterable.
thisElem - element thisElem is used to fill up the resulting iterable if the self iterable is shorter than that
thatElem - element thatElem is used to fill up the resulting iterable if that is shorter than the self iterable
Returns
Sequence((a0,b0), ..., (an,bn), (elem,bn+1), ..., {elem,bm}) when [a0, ..., an] zip [b0, ..., bm] is invoked where m > n.

def zipWithIndex[A1 >: A, That](implicit bf : CanBuildFrom[Repr, (A1, Int), That]) : That
Zips this iterable with its indices (startiong from 0).

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 in the same order.

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

override def canEqual(that : Any) : Boolean
Method called from equality methods, so that user-defined subclasses can refuse to be equal to other collections of the same kind.
Overrides
Equals.canEqual

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

override def view(from : Int, until : Int) : IterableView[A, Repr]
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
TraversableLike.view

@deprecated("use `head' instead")

def first : A

@deprecated("use `headOption' instead")

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

@deprecated("use `view' instead")

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