Scala Vector informational and mathematical methods (syntax, examples)

This page contains a collection of examples of how to use Scala Vector class informational and mathematical methods. Note that these same methods will also work with a Scala Seq, including IndexedSeq.

Informational and mathematical methods

As the name implies, these methods let you get information about the contents of a Vector, or perform mathematical expressions on a Vector.

Method Returns
contains(e) True if the vector contains the element e
containsSlice(s) True if the vector contains the sequence s
count(p) The number of elements in the vector for which the predicate is true
endsWith(s) True if the vector ends with the sequence s
exists(p) True if the predicate returns true for at least one element in the vector
find(p) The first element that matches the predicate p, returned as an Option
forall(p) True if the predicate p is true for all elements in the vector
hasDefiniteSize True if the vector has a finite size
indexOf(e) The index of the first occurrence of the element e in the vector
indexOf(e,i) The index of the first occurrence of the element e in the vector, searching only from the value of the start index i
indexOfSlice(s) The index of the first occurrence of the sequence s in the vector
indexOfSlice(s,i) The index of the first occurrence of the sequence s in the vector, searching only from the value of the start index i
indexWhere(p) The index of the first element where the predicate p returns true
indexWhere(p,i) The index of the first element where the predicate p returns true, searching only from the value of the start index i
isDefinedAt(i) True if the vector contains the index i
isEmpty True if the vector contains no elements
lastIndexOf(e) The index of the last occurrence of the element e in the vector
lastIndexOf(e,i) The index of the last occurrence of the element e in the vector, occurring before or at the index i
lastIndexOfSlice(s) The index of the last occurrence of the sequence s in the vector
lastIndexOfSlice(s,i) The index of the last occurrence of the sequence s in the vector, occurring before or at the index i
lastIndexWhere(p) The index of the first element where the predicate p returns true
lastIndexWhere(p,i) The index of the first element where the predicate p returns true, occurring before or at the index i
max The largest element in the vector
min The smallest element in the vector
nonEmpty True if the vector is not empty (i.e., if it contains 1 or more elements)
product The result of multiplying the elements in the collection
segmentLength(p,i) The length of the longest segment for which the predicate p is true, starting at the index i
size The number of elements in the vector
startsWith(s) True if the vector begins with the elements in the sequence s
startsWith(s,i) True if the vector has the sequence s starting at the index i
sum The sum of the elements in the vector
fold(s)(o) “Fold” the elements of the vector using the binary operator o, using an initial seed s (see also reduce)
foldLeft(s)(o) “Fold” the elements of the vector using the binary operator o, using an initial seed s, going from left to right (see also reduceLeft)
foldRight(s)(o) “Fold” the elements of the vector using the binary operator o, using an initial seed s, going from right to left (see also reduceRight)
reduce “Reduce” the elements of the vector using the binary operator o
reduceLeft “Reduce” the elements of the vector using the binary operator o, going from left to right
reduceRight “Reduce” the elements of the vector using the binary operator o, going from right to left

Examples

First, some sample data:

val evens = Vector(2, 4, 6)
val odds = Vector(1, 3, 5)
val fbb = "foo bar baz"
val firstTen = (1 to 10).toVector          # Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val fiveToFifteen = (5 to 15).toVector     # Vector(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
val empty = Vector[Int]()                  # Vector[Int] = Vector()
val letters = ('a' to 'f').toVector        # Vector(a, b, c, d, e, f)

The examples:

evens.contains(2)                          # true
firstTen.containsSlice(Seq(3,4,5))         # true
firstTen.count(_ % 2 == 0)                 # 5
firstTen.endsWith(Seq(9,10))               # true
firstTen.exists(_ > 10)                    # false
firstTen.find(_ > 2)                       # Some(3)
firstTen.forall(_ < 20)                    # true
firstTen.hasDefiniteSize                   # true
empty.hasDefiniteSize                      # true
letters.indexOf('b')                       # 1 (zero-based)
letters.indexOf('d', 2)                    # 3
letters.indexOf('d', 3)                    # 3
letters.indexOf('d', 4)                    # -1
letters.indexOfSlice(Seq('c','d'))         # 2
letters.indexOfSlice(Seq('c','d'),2)       # 2
letters.indexOfSlice(Seq('c','d'),3)       # -1
firstTen.indexWhere(_ == 3)                # 2
firstTen.indexWhere(_ == 3, 2)             # 2
firstTen.indexWhere(_ == 3, 5)             # -1
letters.isDefinedAt(1)                     # true
letters.isDefinedAt(20)                    # false
letters.isEmpty                            # false
empty.isEmpty                              # true

# lastIndex...
val fbb = "foo bar baz"
fbb.indexOf('a')                           # 5
fbb.lastIndexOf('a')                       # 9
fbb.lastIndexOf('a', 10)                   # 9
fbb.lastIndexOf('a', 9)                    # 9
fbb.lastIndexOf('a', 6)                    # 5
fbb.lastIndexOf('a', 5)                    # 5
fbb.lastIndexOf('a', 4)                    # -1

fbb.lastIndexOfSlice("ar")                 # 5
fbb.lastIndexOfSlice(Seq('a','r'))         # 5
fbb.lastIndexOfSlice(Seq('a','r'), 4)      # -1
fbb.lastIndexOfSlice(Seq('a','r'), 5)      # 5
fbb.lastIndexOfSlice(Seq('a','r'), 6)      # 5

fbb.lastIndexWhere(_ == 'a')               # 9
fbb.lastIndexWhere(_ == 'a', 4)            # -1
fbb.lastIndexWhere(_ == 'a', 5)            # 5
fbb.lastIndexWhere(_ == 'a', 6)            # 5
fbb.lastIndexWhere(_ == 'a', 8)            # 5
fbb.lastIndexWhere(_ == 'a', 9)            # 9

firstTen.max                               # 10
letters.max                                # f
firstTen.min                               # 1
letters.min                                # a
letters.nonEmpty                           # true
empty.nonEmpty                             # false
firstTen.product                           # 3628800
letters.size                               # 6

val x = Vector(1,2,9,1,1,1,1,4)
x.segmentLength(_ < 4, 0)                  # 2
x.segmentLength(_ < 4, 2)                  # 0
x.segmentLength(_ < 4, 3)                  # 4
x.segmentLength(_ < 4, 4)                  # 3

firstTen.startsWith(Seq(1,2))              # true
firstTen.startsWith(Seq(1,2), 0)           # true
firstTen.startsWith(Seq(1,2), 1)           # false
firstTen.sum                               # 55

firstTen.fold(100)(_ + _)                  # 155
firstTen.foldLeft(100)(_ + _)              # 155
firstTen.foldRight(100)(_ + _)             # 155
firstTen.reduce(_ + _)                     # 55
firstTen.reduceLeft(_ + _)                 # 55
firstTen.reduceRight(_ + _)                # 55

firstTen.fold(100)(_ - _)                  # 45
firstTen.foldLeft(100)(_ - _)              # 45
firstTen.foldRight(100)(_ - _)             # 95
firstTen.reduce(_ - _)                     # -53
firstTen.reduceLeft(_ - _)                 # -53
firstTen.reduceRight(_ - _)                # -5

More on fold and reduce

More information

I created this tutorial to show examples of Scala Vector informational and mathematical methods, but for many more examples of how to work with Vector, see my Scala Vector class syntax and method examples tutorial.