Scala IndexedSeq class: methods, examples, and syntax

Summary: This page contains a large collection of examples of how to use the methods on the Scala IndexedSeq class.

Back to top

Scala IndexedSeq class introduction

IndexedSeq is a type of Seq that is indexed, which means that it is very fast for most operations, especially where random access of elements is required. It’s also immutable, which means that you can’t change elements that are in an IndexedSeq, and you also can’t resize it.

IndexedSeq is a trait. Per the Scaladoc, IndexedSeq is “a base trait for indexed sequences. Indexed sequences support constant-time or near constant-time element access and length computation.”

In Scala 2.12.4 — which I use for all of the examples that follow — you’ll see that IndexedSeq is actually backed by Vector:

scala> val nums = IndexedSeq(1, 2, 3)
nums: IndexedSeq[Int] = Vector(1, 2, 3)
                        ------

That is, when you say, “Give me a new IndexedSeq that contains the integers 1-3,” Scala actually creates a Vector to hold your integers.

I generally tend to use the Vector class as an immutable, indexed sequence, but I thought I’d include these IndexedSeq examples for people that might be using it.

Back to top

Important note about the examples

IndexedSeq is immutable, so in all of the examples that follow you need to assign the result of the operation shown to a new variable, like this:

val x = nums.distinct

In an effort to keep the examples smaller and easier to read, I generally don’t do that in the following examples.

Back to top

Create a new IndexedSeq with initial elements

Create an IndexedSeq:

val nums = IndexedSeq(1, 2, 3)          // IndexedSeq[Int] = Vector(1, 2, 3)
val words = IndexedSeq("foo", "bar")    // IndexedSeq[String] = Vector(foo, bar)

When the values in the sequence have mixed/multiple types you may want to specify the type of the sequence:

val x = IndexedSeq(1, 1.0, 1F)                       // IndexedSeq[Double] = IndexedSeq(1.0, 1.0, 1.0)
val x: IndexedSeq[Number] = IndexedSeq(1, 1.0, 1F)   // IndexedSeq[Number] = Vector(1, 1.0, 1.0)

A custom example:

trait Animal
trait Furry
case class Dog(name: String) extends Animal with Furry
case class Cat(name: String) extends Animal with Furry

# (a) resulting type is `IndexedSeq[Product with Serializable with Animal with Furry]`
val animalHouse = IndexedSeq(
    Dog("Rover"),
    Cat("Felix")
)

# (b) be clear you want `IndexedSeq[Animal]`
val animalHouse: IndexedSeq[Animal] = IndexedSeq(
    Dog("Rover"),
    Cat("Felix")
)

If you ever need to create an empty IndexedSeq:

val nums = IndexedSeq[Int]()            // IndexedSeq[Int] = Vector()

Remember the construction syntax is just syntactic sugar for apply:

val nums = IndexedSeq(1, 2, 3)          // IndexedSeq[Int] = Vector(1, 2, 3)
val nums = IndexedSeq.apply(1, 2, 3)    // IndexedSeq[Int] = Vector(1, 2, 3)
Back to top

Create a new IndexedSeq by populating it

You can create a new IndexedSeq that’s populated with initial elements using a Range:

# to, until
(1 to 5).toIndexedSeq                   // IndexedSeq[Int] = Range 1 to 5
(1 until 5).toIndexedSeq                // IndexedSeq[Int] = Range 1 to 5

(1 to 10 by 2).toIndexedSeq             // IndexedSeq[Int] = inexact Range 1 to 10 by 2
(1 until 10 by 2).toIndexedSeq          // IndexedSeq[Int] = inexact Range 1 until 10 by 2
(1 to 10).by(2).toIndexedSeq            // IndexedSeq[Int] = inexact Range 1 to 10 by 2

('d' to 'h').toIndexedSeq               // IndexedSeq[Char] = NumericRange d to h
('d' until 'h').toIndexedSeq            // IndexedSeq[Char] = NumericRange d until h

('a' to 'f').by(2).toIndexedSeq         // IndexedSeq[Char] = NumericRange a to f by ?

# range method
IndexedSeq.range(1, 3)                  // IndexedSeq[Int] = Vector(1, 2)
IndexedSeq.range(1, 6, 2)               // IndexedSeq[Int] = Vector(1, 3, 5)

You can also use the fill and tabulate methods:

IndexedSeq.fill(3)("foo")               // IndexedSeq[String] = Vector(foo, foo, foo)
IndexedSeq.tabulate(3)(n => n * n)      // IndexedSeq[Int] = Vector(0, 1, 4)
IndexedSeq.tabulate(4)(n => n * n)      // IndexedSeq[Int] = Vector(0, 1, 4, 9)
Back to top

How to add (append and prepend) elements to a IndexedSeq

Because IndexedSeq is immutable, you can’t add elements to an existing IndexedSeq. The way you work with IndexedSeq is to modify the elements it contains as you assign the results to a new IndexedSeq.

Method Description Example
:+ append 1 item oldIndexedSeq :+ e
++ append N items oldIndexedSeq ++ newIndexedSeq
+: prepend 1 item e +: oldIndexedSeq
++: prepend N items newIndexedSeq ++: oldIndexedSeq

Again, you can use these methods, but it’s not recommended.

Append and prepend examples

These examples show how to use those methods to append and prepend elements to an IndexedSeq:

val v1 = IndexedSeq(4,5,6)         // IndexedSeq[Int] = Vector(4, 5, 6)
val v2 = v1 :+ 7                   // Vector(4, 5, 6, 7)
val v3 = v2 ++ IndexedSeq(8,9)     // Vector(4, 5, 6, 7, 8, 9)

val v4 = 3 +: v3                   // Vector(3, 4, 5, 6, 7, 8, 9)
val v5 = IndexedSeq(1,2) ++: v4    // Vector(1, 2, 3, 4, 5, 6, 7, 8, 9)

About the : character in the method names

Note that during these operations the : character is always next to the old (original) sequence. I use that as a way to remember these methods.

The correct technical way to think about this is that a Scala method name that ends with the : character is right-associative, meaning that the method comes from the variable on the right side of the expression. Therefore, with +: and ++:, these methods comes from the IndexedSeq that’s on the right of the method name.

Back to top

Filtering methods (how to “remove” elements from a IndexedSeq)

A IndexedSeq is an immutable sequence, so you don’t remove elements from it. Instead, you describe how to remove elements as you assign the results to a new collection. These methods let you “remove” elements during this process:

Method Description
distinct Return a new sequence with no duplicate elements
drop(n) Return all elements after the first n elements
dropRight(n) Return all elements except the last n elements
dropWhile(p) Drop the first sequence of elements that matches the predicate p
filter(p) Return all elements that match the predicate p
filterNot(p) Return all elements that do not match the predicate p
find(p) Return the first element that matches the predicate p
head Return the first element; can throw an exception if the IndexedSeq is empty
headOption Returns the first element as an Option
init All elements except the last one
intersect(s) Return the intersection of the sequence and another sequence s
last The last element; can throw an exception if the IndexedSeq is empty
lastOption The last element as an Option
slice(f,u) A sequence of elements from index f (from) to index u (until)
tail All elements after the first element
take(n) The first n elements
takeRight(n) The last n elements
takeWhile(p) The first subset of elements that matches the predicate p

Examples

val a = IndexedSeq(10, 20, 30, 40, 10)   // Vector(10, 20, 30, 40, 10)
a.distinct                            // Vector(10, 20, 30, 40)
a.drop(2)                             // Vector(30, 40, 10)
a.dropRight(2)                        // Vector(10, 20, 30)
a.dropWhile(_ < 25)                   // Vector(30, 40, 10)
a.filter(_ < 25)                      // Vector(10, 20, 10)
a.filter(_ > 100)                     // Vector()
a.filterNot(_ < 25)                   // Vector(30, 40)
a.find(_ > 20)                        // Some(30)
a.head                                // 10
a.headOption                          // Some(10)
a.init                                // Vector(10, 20, 30, 40)
a.intersect(IndexedSeq(19,20,21))     // Vector(20)
a.last                                // 10
a.lastOption                          // Some(10)
a.slice(2,4)                          // Vector(30, 40)
a.tail                                // Vector(20, 30, 40, 10)
a.take(3)                             // Vector(10, 20, 30)
a.takeRight(2)                        // Vector(40, 10)
a.takeWhile(_ < 30)                   // Vector(10, 20)

As noted, head and last can throw exceptions:

scala> val a = IndexedSeq[Int]()
a: IndexedSeq[Int] = Vector()

scala> a.head
java.lang.UnsupportedOperationException: empty.head
  at scala.collection.immutable.Vector.head(Vector.scala:185)
  ... 28 elided

scala> a.last
java.lang.UnsupportedOperationException: empty.last
  at scala.collection.immutable.Vector.last(Vector.scala:197)
  ... 28 elided
Back to top

How to “update” IndexedSeq elements

Because IndexedSeq is immutable, you can’t update elements in place, but depending on your definition of “update,” there are a variety of methods that let you update a IndexedSeq as you assign the result to a new variable:

Method Returns
collect(pf) A new collection by applying the partial function pf to all elements of the sequence, returning elements for which the function is defined
distinct A new sequence with no duplicate elements
flatten Transforms a sequence of sequences into a single sequence
flatMap(f) When working with sequences, it works like map followed by flatten
map(f) Return a new sequence by applying the function f to each element in the IndexedSeq
updated(i,v) A new sequence with the element at index i replaced with the new value v
union(s) A new sequence that contains elements from the current sequence and the sequence s
val x = IndexedSeq(Some(1), None, Some(3), None)   // IndexedSeq[Option[Int]] = Vector(Some(1), None, Some(3), None)

x.collect{case Some(i) => i}              // IndexedSeq(1, 3)

val x = IndexedSeq(1,2,1,2)
x.distinct                                // Vector(1, 2)
x.map(_ * 2)                              // Vector(2, 4, 2, 4)
x.updated(0,100)                          // Vector(100, 2, 1, 2)

val a = IndexedSeq(IndexedSeq(1,2), IndexedSeq(3,4))
a.flatten                                 // Vector(1, 2, 3, 4)

val fruits = IndexedSeq("apple", "pear")
fruits.map(_.toUpperCase)                 // Vector(APPLE, PEAR)
fruits.flatMap(_.toUpperCase)             // Vector(A, P, P, L, E, P, E, A, R)

IndexedSeq(2,4).union(IndexedSeq(1,3))    // Vector(2, 4, 1, 3)
Back to top

Transformer methods

A transformer method is a method that constructs a new collection from an existing collection.

Method Returns
collect(pf) Creates a new collection by applying the partial function pf to all elements of the sequence, returning elements for which the function is defined
diff(c) The difference between this sequence and the collection c
distinct A new sequence with no duplicate elements
flatten Transforms a sequence of sequences into a single sequence
flatMap(f) When working with sequences, it works like map followed by flatten
map(f) A new sequence by applying the function f to each element in the IndexedSeq
reverse A new sequence with the elements in reverse order
sortWith(f) A new sequence with the elements sorted with the use of the function f
updated(i,v) A new IndexedSeq with the element at index i replaced with the new value v
union(c) A new sequence that contains all elements of the sequence and the collection c
zip(c) A collection of pairs by matching the sequence with the elements of the collection c
zipWithIndex A sequence of each element contained in a tuple along with its index
val x = IndexedSeq(Some(1), None, Some(3), None)

x.collect{case Some(i) => i}                // Vector(1, 3)

# diff
val oneToFive = (1 to 5).toIndexedSeq       // IndexedSeq[Int] = Range 1 to 5
val threeToSeven = (3 to 7).toIndexedSeq    // IndexedSeq[Int] = Range 3 to 7
oneToFive.diff(threeToSeven)                // IndexedSeq[Int] = Vector(1, 2)
threeToSeven.diff(oneToFive)                // IndexedSeq[Int] = Vector(6, 7)

IndexedSeq(1,2,1,2).distinct                // IndexedSeq[Int] = Vector(1, 2)

val a = IndexedSeq(IndexedSeq(1,2), IndexedSeq(3,4))
a.flatten                                   // IndexedSeq[Int] = Vector(1, 2, 3, 4)

# map, flatMap
val fruits = IndexedSeq("apple", "pear")
fruits.map(_.toUpperCase)                   // Vector(APPLE, PEAR)
fruits.flatMap(_.toUpperCase)               // Vector(A, P, P, L, E, P, E, A, R)

IndexedSeq(1,2,3).reverse                   // Vector(3, 2, 1)

val nums = IndexedSeq(10, 5, 8, 1, 7)
nums.sorted                                 // Vector(1, 5, 7, 8, 10)
nums.sortWith(_ < _)                        // Vector(1, 5, 7, 8, 10)
nums.sortWith(_ > _)                        // Vector(10, 8, 7, 5, 1)

IndexedSeq(1,2,3).updated(0,10)             // Vector(10, 2, 3)
IndexedSeq(2,4).union(IndexedSeq(1,3))      // Vector(2, 4, 1, 3)

# zip
val women = IndexedSeq("Wilma", "Betty")    // Vector(Wilma, Betty)
val men = IndexedSeq("Fred", "Barney")      // Vector(Fred, Barney)
val couples = women.zip(men)                // Vector((Wilma,Fred), (Betty,Barney))

val a = IndexedSeq.range('a', 'e')          // Vector(a, b, c, d)
a.zipWithIndex                              // Vector((a,0), (b,1), (c,2), (d,3))
Back to top

Informational and mathematical methods

These methods let you obtain information from a collection.

Method Returns
contains(e) True if the sequence contains the element e
containsSlice(s) True if the sequence contains the sequence s
count(p) The number of elements in the sequence for which the predicate is true
endsWith(s) True if the sequence ends with the sequence s
exists(p) True if the predicate returns true for at least one element in the sequence
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 sequence
hasDefiniteSize True if the sequence has a finite size
indexOf(e) The index of the first occurrence of the element e in the sequence
indexOf(e,i) The index of the first occurrence of the element e in the sequence, searching only from the value of the start index i
indexOfSlice(s) The index of the first occurrence of the sequence s in the sequence
indexOfSlice(s,i) The index of the first occurrence of the sequence s in the sequence, 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 sequence contains the index i
isEmpty True if the sequence contains no elements
lastIndexOf(e) The index of the last occurrence of the element e in the sequence
lastIndexOf(e,i) The index of the last occurrence of the element e in the sequence, occurring before or at the index i
lastIndexOfSlice(s) The index of the last occurrence of the sequence s in the sequence
lastIndexOfSlice(s,i) The index of the last occurrence of the sequence s in the sequence, 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 sequence
min The smallest element in the sequence
nonEmpty True if the sequence 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 sequence
startsWith(s) True if the sequence begins with the elements in the sequence s
startsWith(s,i) True if the sequence has the sequence s starting at the index i
sum The sum of the elements in the sequence
fold(s)(o) “Fold” the elements of the sequence using the binary operator o, using an initial seed s (see also reduce)
foldLeft(s)(o) “Fold” the elements of the sequence 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 sequence using the binary operator o, using an initial seed s, going from right to left (see also reduceRight)
reduce “Reduce” the elements of the sequence using the binary operator o
reduceLeft “Reduce” the elements of the sequence using the binary operator o, going from left to right
reduceRight “Reduce” the elements of the sequence using the binary operator o, going from right to left

Examples

First, some sample data:

val evens = IndexedSeq(2, 4, 6)               // Vector(2, 4, 6)
val odds = IndexedSeq(1, 3, 5)                // Vector(1, 3, 5)
val fbb = "foo bar baz"                       // String = foo bar baz
val firstTen = (1 to 10).toIndexedSeq         // IndexedSeq[Int] = Range 1 to 10
val fiveToFifteen = (5 to 15).toIndexedSeq    // IndexedSeq[Int] = Range 5 to 15
val empty = IndexedSeq[Int]()                 // Vector()
val letters = ('a' to 'f').toIndexedSeq       // IndexedSeq[Char] = NumericRange a to f

The examples:

evens.contains(2)                           // true
firstTen.containsSlice(IndexedSeq(3,4,5))   // true
firstTen.count(_ % 2 == 0)                  // 5
firstTen.endsWith(IndexedSeq(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(IndexedSeq('c','d'))     // 2
letters.indexOfSlice(IndexedSeq('c','d'),2)   // 2
letters.indexOfSlice(IndexedSeq('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(IndexedSeq('a','r'))      // 5
fbb.lastIndexOfSlice(IndexedSeq('a','r'), 4)   // -1
fbb.lastIndexOfSlice(IndexedSeq('a','r'), 5)   // 5
fbb.lastIndexOfSlice(IndexedSeq('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 = IndexedSeq(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(IndexedSeq(1,2))        // true
firstTen.startsWith(IndexedSeq(1,2), 0)     // true
firstTen.startsWith(IndexedSeq(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

Note: Methods like foldRight and reduceRight are not recommended with IndexedSeq because they will be very slow for large collections.

More on fold and reduce

Back to top

Grouping methods

These methods generally let you create multiple groups from a collection.

Method Returns
groupBy(f) A map of collections created by the function f
grouped Breaks the sequence into fixed-size iterable collections
partition(p) Two collections created by the predicate p
sliding(i,s) Group elements into fixed size blocks by passing a sliding window of size i and step s over them
span(p) A collection of two collections; the first created by sequence.takeWhile(p), and the second created by sequence.dropWhile(p)
splitAt(i) A collection of two collections by splitting the sequence at index i
unzip The opposite of zip, breaks a collection into two collections by dividing each element into two pieces; such as breaking up a sequence of Tuple2 elements

Examples

val firstTen = (1 to 10).toIndexedSeq    // IndexedSeq[Int] = Range 1 to 10

firstTen.groupBy(_ > 5)                  // IndexedSeq[Int]] = Map(false -> Vector(1, 2, 3, 4, 5), true -> Vector(6, 7, 8, 9, 10))
firstTen.grouped(2)                      // IndexedSeq[Int]] = non-empty iterator
firstTen.grouped(2).toIndexedSeq         // Vector(Vector(1, 2), Vector(3, 4), Vector(5, 6), Vector(7, 8), Vector(9, 10))
firstTen.grouped(5).toIndexedSeq         // Vector(Vector(1, 2, 3, 4, 5), Vector(6, 7, 8, 9, 10))

"foo bar baz".partition(_ < 'c')         // (" ba ba", foorz)  // a Tuple2
firstTen.partition(_ > 5)                // (Vector(6, 7, 8, 9, 10),Vector(1, 2, 3, 4, 5))

firstTen.sliding(2)                      // IndexedSeq[Int]] = non-empty iterator
firstTen.sliding(2).toIndexedSeq         // Vector(Vector(1, 2), Vector(2, 3), Vector(3, 4), Vector(4, 5), Vector(5, 6), Vector(6, 7), Vector(7, 8), Vector(8, 9), Vector(9, 10))
firstTen.sliding(2,2).toIndexedSeq       // Vector(Vector(1, 2), Vector(3, 4), Vector(5, 6), Vector(7, 8), Vector(9, 10))
firstTen.sliding(2,3).toIndexedSeq       // Vector(Vector(1, 2), Vector(4, 5), Vector(7, 8), Vector(10))
firstTen.sliding(2,4).toIndexedSeq       // Vector(Vector(1, 2), Vector(5, 6), Vector(9, 10))

val x = IndexedSeq(15, 10, 5, 8, 20, 12)
x.groupBy(_ > 10)                        // Map[Boolean,IndexedSeq[Int]] = Map(false -> Vector(10, 5, 8), true -> Vector(15, 20, 12))
x.partition(_ > 10)                      // (Vector(15, 20, 12),Vector(10, 5, 8))
x.span(_ < 20)                           // (Vector(15, 10, 5, 8),Vector(20, 12))
x.splitAt(2)                             // (Vector(15, 10),Vector(5, 8, 20, 12))

More information:

Back to top

Looping over a IndexedSeq with for and foreach

These examples show how to loop/iterate over a vector with for and foreach. (As I write in Functional Programming, Simplified, foreach is only used for side effects, and therefore I rarely use it.)

val oneToFive = IndexedSeq(1, 2, 3, 4, 5)   // Vector(1, 2, 3, 4, 5)

for (i <- oneToFive) yield i          // Vector(1, 2, 3, 4, 5)
for (i <- oneToFive) yield i * 2      // Vector(2, 4, 6, 8, 10)
for (i <- oneToFive) yield i % 2      // Vector(1, 0, 1, 0, 1)

for {                                 // Vector(3, 4, 5)
    i <- oneToFive
    if i > 2
} yield i

for {                                 // Vector(6, 8, 10)
    i <- oneToFive
    if i > 2
} yield {
    // could be multiple lines here
    i * 2
}

# foreach (which i rarely use)
val oneToThree = IndexedSeq(1, 2, 3)
oneToThree.foreach(print)            // 123
for (i <- oneToThree) print(i)       // 123
Back to top

A few things you can do with a IndexedSeq of Options

The Option type is used a lot in idiomatic Scala code, so here are some ways to work with a Vector that contains Options.

val x = IndexedSeq(Some(1), None, Some(3), None)   // Vector(Some(1), None, Some(3), None)

x.flatten                                    // Vector(1, 3)
x.collect{case Some(i) => i}                 // Vector(1, 3)

# map, flatten, flatMap
import scala.util.Try
def toInt(s: String): Option[Int] = Try(Integer.parseInt(s)).toOption
val strings = IndexedSeq("1", "2", "foo", "3", "bar")

strings.map(toInt)                   // Vector(Some(1), Some(2), None, Some(3), None)
strings.map(toInt).flatten           // Vector(1, 2, 3)
strings.flatMap(toInt)               // Vector(1, 2, 3)
Back to top

Scala IndexedSeq summary

In summary, I hope these IndexedSeq examples are helpful. If I made a mistake, or you know another way to do something with an IndexedSeq I haven’t shown, leave a note in the Comments section.

Back to top

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly, and which you can find on Amazon.com:

I hope it has been helpful. All the best, Al.

Back to top

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.