scala.collection

trait SeqLike

[source: scala/collection/SeqLike.scala]

trait SeqLike[+A, +Repr]
extends IterableLike[A, Repr]
Class Seq[A] represents sequences of elements of type A. It adds the following methods to class Iterable: `length`, `lengthCompare`, `apply`, `isDefinedAt`, `segmentLength`, `prefixLength`, `indexWhere`, `indexOf`, `lastIndexWhere`, `lastIndexOf`, `reverse`, `reverseIterator`, `startsWith`, `endsWith`, `indexOfSeq`, , `zip`, `zipAll`, `zipWithIndex`.
Author
Martin Odersky
Matthias Zenger
Version
1.0, 16/07/2003
Since
2.8
Direct Known Subclasses:
IndexedSeqLike, LinearSeqLike, Seq, SeqProxyLike, SeqViewLike, Seq, Vector, BufferLike, LinkedListLike, Seq, NodeSeq

Method Summary
def +: [B >: A, That](elem : B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Returns a new sequence consisting of `elem` followed by the elements of this sequence.
def :+ [B >: A, That](elem : B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Returns a new sequence consisting of the elements of this sequence followed by `elem`.
abstract def apply (idx : Int) : A
Returns the elements at position `idx`
def contains (elem : Any) : Boolean
Tests if the given value elem is a member of this sequence.
def containsSlice [B](that : Seq[B]) : Boolean
Is that a slice in this?
def diff [B >: A, That](that : Seq[B]) : Repr
def endsWith [B](that : Seq[B]) : Boolean
override def equals (that : Any) : Boolean
The equality method defined in `AnyRef`.
def equalsWith [B](that : Seq[B])(f : (A, B) => Boolean) : Boolean
def findIndexOf (p : (A) => Boolean) : Int
Returns index of the first element satisying a predicate, or -1.
def findLastIndexOf (p : (A) => Boolean) : Int
Returns index of the last element satisying a predicate, or -1.
override def hashCode : Int
Returns a hash code value for the object.
def indexOf [B >: A](elem : B, from : Int) : Int
Returns the index of the first occurence of the specified object in this sequence, starting from a start index, or -1, if none exists.
def indexOf [B >: A](elem : B) : Int
Returns the index of the first occurence of the specified object in this sequence.
def indexOfSeq [B >: A](that : Seq[B]) : Int
def indexOfSeq [B >: A](that : Seq[B], fromIndex : Int) : Int
def indexWhere (p : (A) => Boolean) : Int
Returns index of the first element satisfying a predicate, or -1, if none exists.
def indexWhere (p : (A) => Boolean, from : Int) : Int
Returns index of the first element starting from a start index satisying a predicate, or -1, if none exists.
def indices : Range
The range of all indices of this sequence.
def intersect [B >: A, That](that : Seq[B]) : Repr
def isDefinedAt (x : Int) : Boolean
Is this partial function defined for the index x?
def lastIndexOf [B >: A](elem : B, end : Int) : Int
Returns the index of the last occurence of the specified element in this sequence before or at a given end index, or -1 if the sequence does not contain this element.
def lastIndexOf [B >: A](elem : B) : Int
Returns the index of the last occurence of the specified element in this sequence, or -1 if the sequence does not contain this element.
def lastIndexOfSeq [B >: A](that : Seq[B], fromIndex : Int) : Int
def lastIndexOfSeq [B >: A](that : Seq[B]) : Int
def lastIndexWhere (p : (A) => Boolean, end : Int) : Int
Returns index of the last element not exceeding a given end index and satisying a predicate, or -1 if none exists.
def lastIndexWhere (p : (A) => Boolean) : Int
Returns index of the last element satisying a predicate, or -1, if none exists.
abstract def length : Int
Returns the length of the sequence.
def lengthCompare (len : Int) : Int
Result of comparing length with operand len. returns x where x < 0 iff this.length < len x == 0 iff this.length == len x > 0 iff this.length > len. The method as implemented here does not call length directly; its running time is O(length min len) instead of O(length). The method should be overwritten if computing length is cheap.
def padTo [B >: A, That](len : Int, elem : B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Returns a new sequence of given length containing the elements of this sequence followed by zero or more occurrences of given elements.
def patch [B >: A, That](from : Int, patch : Seq[B], replaced : Int)(implicit bf : CanBuildFrom[Repr, B, That]) : That
A new sequence, consisting of all elements of current sequence except that `replaced` elements starting from `from` are replaced by `patch`.
def prefixLength (p : (A) => Boolean) : Int
Returns length of longest prefix of this seqence such that every element of the prefix satisfies predicate `p`.
override def projection : SeqView[A, Repr]
returns a projection that can be used to call non-strict filter, map, and flatMap methods that build projections of the collection.
def removeDuplicates : Repr
Builds a new sequence from this sequence in which any duplicates (wrt to ==) removed. Among duplicate elements, only the first one is retained in the result sequence
def reverse : Repr
A sequence of type C consisting of all elements of this sequence in reverse order.
def reverseIterator : Iterator[A]
The elements of this sequence in reversed order
def reverseMap [B, That](f : (A) => B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Apply a function to all the elements of the sequence, and return the reversed sequence of results. This is equivalent to a call to reverse followed by a call to map, but more efficient.
def reversedElements : Iterator[A]
def segmentLength (p : (A) => Boolean, from : Int) : Int
Returns length of longest segment starting from a start index `from` such that every element of the segment satisfies predicate `p`.
override def size : Int
Should always be length
def sortBy [B](f : (A) => B)(implicit ord : Ordering[B]) : Repr
Sort the sequence according to the Ordering which results from transforming the implicitly given Ordering[B] to an Ordering[A]. For example: val words = "The quick brown fox jumped over the lazy dog".split(' ') // this works because scala.Ordering will implicitly provide an Ordering[Tuple2[Int, Char]] words.sortBy(x => (x.length, x.head)) res0: Array[String] = Array(The, dog, fox, the, lazy, over, brown, quick, jumped)
def sortWith [B >: A](ord : Ordering[B]) : Repr
def sortWith (lt : (A, A) => Boolean) : Repr
Sort the sequence according to the comparison function lt(e1: a, e2: a) => Boolean, which should be true iff e1 precedes e2 in the desired ordering. The sort is stable. That is elements that are equal wrt `lt` appear in the same order in the sorted sequence as in the original.
def startsWith [B](that : Seq[B], offset : Int) : Boolean
Checks whether the argument sequence is contained at the specified index within the receiver object. If the both the receiver object, this and the argument, that are infinite sequences this method may not terminate.
def startsWith [B](that : Seq[B]) : Boolean
Check whether the receiver object starts with the argument sequence.
protected[this] override def thisCollection : Seq[A]
protected[this] override def toCollection (repr : Repr) : Seq[A]
override def toSeq : Seq[A]
Overridden for efficiency.
override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.
def union [B >: A, That](that : Seq[B])(implicit bf : CanBuildFrom[Repr, B, That]) : That
def updated [B >: A, That](index : Int, elem : B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Returns a copy of this sequence with the element at position `index` replaced by `elem`.
override def view (from : Int, until : Int) : SeqView[A, Repr]
A sub-iterable view starting at index `from` and extending up to (but not including) index `until`.
override def view : SeqView[A, Repr]
Creates a view of this iterable @see IterableView
Methods inherited from IterableLike
iterator (abstract), elements, foreach, forall, exists, find, isEmpty, foldRight, reduceRight, toIterable, head, take, slice, takeWhile, takeRight, dropRight, copyToArray, zip, zipAll, zipWithIndex, sameElements, toStream, canEqual, first, firstOption
Methods inherited from TraversableLike
newBuilder (abstract), repr, nonEmpty, 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, toIndexedSeq, toSet, mkString, mkString, mkString, addString, addString, addString, stringPrefix, withFilter
Methods inherited from AnyRef
getClass, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
protected[this] override def thisCollection : Seq[A]
Overrides
IterableLike.thisCollection

protected[this] override def toCollection(repr : Repr) : Seq[A]
Overrides
IterableLike.toCollection

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

abstract def apply(idx : Int) : A
Returns the elements at position `idx`

def lengthCompare(len : Int) : Int
Result of comparing length with operand len. returns x where x < 0 iff this.length < len x == 0 iff this.length == len x > 0 iff this.length > len. The method as implemented here does not call length directly; its running time is O(length min len) instead of O(length). The method should be overwritten if computing length is cheap.

override def size : Int
Should always be length

def isDefinedAt(x : Int) : Boolean
Is this partial function defined for the index x?

def segmentLength(p : (A) => Boolean, from : Int) : Int
Returns length of longest segment starting from a start index `from` such that every element of the segment satisfies predicate `p`.
Notes
may not terminate for infinite-sized collections.
Parameters
p - the predicate
from - the start index

def prefixLength(p : (A) => Boolean) : Int
Returns length of longest prefix of this seqence such that every element of the prefix satisfies predicate `p`.
Notes
may not terminate for infinite-sized collections.
Parameters
p - the predicate

def indexWhere(p : (A) => Boolean) : Int
Returns index of the first element satisfying a predicate, or -1, if none exists.
Notes
may not terminate for infinite-sized collections.
Parameters
p - the predicate

def indexWhere(p : (A) => Boolean, from : Int) : Int
Returns index of the first element starting from a start index satisying a predicate, or -1, if none exists.
Notes
may not terminate for infinite-sized collections.
Parameters
p - the predicate
from - the start index

@deprecated("Use `indexWhere' instead")

def findIndexOf(p : (A) => Boolean) : Int
Returns index of the first element satisying a predicate, or -1.

def indexOf[B >: A](elem : B) : Int
Returns the index of the first occurence of the specified object in this sequence.
Notes
may not terminate for infinite-sized collections.
Parameters
elem - element to search for.
Returns
the index in this sequence of the first occurence of the specified element, or -1 if the sequence does not contain this element.

def indexOf[B >: A](elem : B, from : Int) : Int
Returns the index of the first occurence of the specified object in this sequence, starting from a start index, or -1, if none exists.
Notes
may not terminate for infinite-sized collections.
Parameters
elem - element to search for.

def lastIndexOf[B >: A](elem : B) : Int
Returns the index of the last occurence of the specified element in this sequence, or -1 if the sequence does not contain this element.
Parameters
elem - element to search for.
Returns
the index in this sequence of the last occurence of the specified element, or -1 if the sequence does not contain this element.

def lastIndexOf[B >: A](elem : B, end : Int) : Int
Returns the index of the last occurence of the specified element in this sequence before or at a given end index, or -1 if the sequence does not contain this element.
Parameters
elem - element to search for.
end - the end index

def lastIndexWhere(p : (A) => Boolean) : Int
Returns index of the last element satisying a predicate, or -1, if none exists.
Parameters
p - the predicate
Returns
the index of the last element satisfying p, or -1 if such an element does not exist

def lastIndexWhere(p : (A) => Boolean, end : Int) : Int
Returns index of the last element not exceeding a given end index and satisying a predicate, or -1 if none exists.
Parameters
end - the end index
p - the predicate

def reverse : Repr
A sequence of type C consisting of all elements of this sequence in reverse order.

def reverseMap[B, That](f : (A) => B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Apply a function to all the elements of the sequence, and return the reversed sequence of results. This is equivalent to a call to reverse followed by a call to map, but more efficient.
Parameters
f - the function to apply to each elements.
Returns
the reversed seq of results.

def reverseIterator : Iterator[A]
The elements of this sequence in reversed order

@deprecated("use `reverseIterator' instead")

def reversedElements : Iterator[A]

def startsWith[B](that : Seq[B], offset : Int) : Boolean
Checks whether the argument sequence is contained at the specified index within the receiver object. If the both the receiver object, this and the argument, that are infinite sequences this method may not terminate.
Returns
true if that is contained in this, at the specified index, otherwise false
See Also
String.startsWith

def startsWith[B](that : Seq[B]) : Boolean
Check whether the receiver object starts with the argument sequence.
Returns
true if that is a prefix of this, otherwise false

def endsWith[B](that : Seq[B]) : Boolean
Returns
true if this sequence end with that sequence
See Also
String.endsWith

def indexOfSeq[B >: A](that : Seq[B]) : Int
Returns
-1 if that not contained in this, otherwise the first index where that is contained.

def indexOfSeq[B >: A](that : Seq[B], fromIndex : Int) : Int

def lastIndexOfSeq[B >: A](that : Seq[B]) : Int
Returns
-1 if that not contained in this, otherwise the last index where that is contained.
Notes
may not terminate for infinite-sized collections.

def lastIndexOfSeq[B >: A](that : Seq[B], fromIndex : Int) : Int

def contains(elem : Any) : Boolean
Tests if the given value elem is a member of this sequence.
Parameters
elem - element whose membership has to be tested.
Returns
true iff there is an element of this sequence which is equal (w.r.t. ==) to elem.

def union[B >: A, That](that : Seq[B])(implicit bf : CanBuildFrom[Repr, B, That]) : That

Computes the multiset union of this sequence and the given sequence that. For example:

      val xs = List(1, 1, 2)
      val ys = List(1, 2, 2, 3)
      println(xs union ys)  // prints "List(1, 1, 2, 1, 2, 2, 3)"
      println(ys union xs)  // prints "List(1, 2, 2, 3, 1, 1, 2)"
    
Parameters
that - the sequence of elements to add to the sequence.
Returns
a sequence containing the elements of this sequence and those of the given sequence that.

def diff[B >: A, That](that : Seq[B]) : Repr

Computes the multiset difference between this sequence and the given sequence that. If an element appears more than once in both sequences, the difference contains m copies of that element, where m is the difference between the number of times the element appears in this sequence and the number of times it appears in that. For example:

      val xs = List(1, 1, 2)
      val ys = List(1, 2, 2, 3)
      println(xs diff ys)  // prints "List(1)"
      println(xs -- ys)    // prints "List()"
    
Parameters
that - the sequence of elements to remove from this sequence.
Returns
the sequence of elements contained only in this sequence plus m copies of each element present in both sequences, where m is defined as above.

def intersect[B >: A, That](that : Seq[B]) : Repr

Computes the multiset intersection between this sequence and the given sequence that; the intersection contains m copies of an element contained in both sequences, where m is the smaller of the number of times the element appears in this sequence or in that. For example:

      val xs = List(1, 1, 2)
      val ys = List(3, 2, 2, 1)
      println(xs intersect ys)  // prints "List(1, 2)"
      println(ys intersect xs)  // prints "List(2, 1)"
    
Parameters
that - the sequence to intersect.
Returns
the sequence of elements contained both in this sequence and in the given sequence that.

def removeDuplicates : Repr
Builds a new sequence from this sequence in which any duplicates (wrt to ==) removed. Among duplicate elements, only the first one is retained in the result sequence

def patch[B >: A, That](from : Int, patch : Seq[B], replaced : Int)(implicit bf : CanBuildFrom[Repr, B, That]) : That
A new sequence, consisting of all elements of current sequence except that `replaced` elements starting from `from` are replaced by `patch`.

def updated[B >: A, That](index : Int, elem : B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Returns a copy of this sequence with the element at position `index` replaced by `elem`.

def +:[B >: A, That](elem : B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Returns a new sequence consisting of `elem` followed by the elements of this sequence.

def :+[B >: A, That](elem : B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
Returns a new sequence consisting of the elements of this sequence followed by `elem`.

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

def sortWith(lt : (A, A) => Boolean) : Repr
Sort the sequence according to the comparison function lt(e1: a, e2: a) => Boolean, which should be true iff e1 precedes e2 in the desired ordering. The sort is stable. That is elements that are equal wrt `lt` appear in the same order in the sorted sequence as in the original.
Parameters
lt - the comparison function
Returns
a sequence sorted according to the comparison function lt(e1: a, e2: a) => Boolean.
Examples
      List("Steve", "Tom", "John", "Bob")
        .sortWith((e1, e2) => (e1 compareTo e2) < 0) =
      List("Bob", "John", "Steve", "Tom")

def sortWith[B >: A](ord : Ordering[B]) : Repr

def sortBy[B](f : (A) => B)(implicit ord : Ordering[B]) : Repr
Sort the sequence according to the Ordering which results from transforming the implicitly given Ordering[B] to an Ordering[A]. For example: val words = "The quick brown fox jumped over the lazy dog".split(' ') // this works because scala.Ordering will implicitly provide an Ordering[Tuple2[Int, Char]] words.sortBy(x => (x.length, x.head)) res0: Array[String] = Array(The, dog, fox, the, lazy, over, brown, quick, jumped)
Parameters
f - the transformation function A => B
ord - the Ordering[B]
Returns
the sorted representation

override def toSeq : Seq[A]
Overridden for efficiency.
Returns
the sequence itself

def indices : Range
The range of all indices of this sequence.

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

override def view(from : Int, until : Int) : SeqView[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
IterableLike.view

override def hashCode : Int
Returns a hash code value for the object.

The default hashing algorithm is platform dependent. Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

Returns
the hash code value for the object.


override def equals(that : Any) : Boolean
The equality method defined in `AnyRef`.

override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.

@deprecated("use `lastIndexWhere' instead")

def findLastIndexOf(p : (A) => Boolean) : Int
Returns index of the last element satisying a predicate, or -1.

@deprecated("Should be replaced by <code>(s1, s2) forall { case (x, y) => f(x, y) }</code>")

def equalsWith[B](that : Seq[B])(f : (A, B) => Boolean) : Boolean

@deprecated("Should be replaced by <code>indexOfSeq(that) != -1</code>")

def containsSlice[B](that : Seq[B]) : Boolean
Is that a slice in this?

@deprecated("use `view' instead")

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