Scala For loops and comprehensions

Question Click to View Answer

Use a for loop to sum all the items in the following collection.

var total = 0
val items = Array(1, 10, 100, 1000)

for (item <- items) total += item

The result should be 1111.

Use a for loop to sum the numbers from 1 to 4 (the result should be 10).

var total = 0
for (i <- Range(0, 5)) {
  total = total + i
}

Range(0, 5) returns a scala.collection.immutable.Range.Exclusive object.

You can inspect the content of the range with Range(0, 5).toList and see it contains List(0, 1, 2, 3, 4).

Use a for loop to sum all the numbers in the nested arrays (result should be 21).

val multi = Array(Array(1, 2), Array(3, 4), Array(5, 6))
var total = 0
for (arr <- multi; i <- arr) total = total + i

Use a for loop to sum all of the even numbers in the following nested arrays.

val multi = Array(Array(1, 2), Array(3, 4), Array(5, 6))
var total = 0
for (arr <- multi; i <- arr; if i % 2 == 0) total = total + i

Scala for loops allow you to express nested loop iteration on a single line.

Use a for loop to square all of the elements in the following array.

val a = Array(1, 2, 3, 4)
for (i <- a) yield i * i

Use a for loop to convert Array(1, 2, 3, 4) to Array("hi 1", "hi 2", "hi 3", "hi 4").

val a = Array(1, 2, 3, 4)
for (i <- a) yield "hi " + i

Use a for loop to convert Array(1, 2, 3, 4) to Array("hi 2", "hi 4"). Filter out all the values that are odd.

val a = Array(1, 2, 3, 4)
for (i <- a if i % 2 == 0) yield "hi " + i

Use a for loop to flatten the following two arrays into a single array.

val a = Array(1, 2)
val b = Array("hello", "world")
// add your code here
// desired result: Array("hello1", "world1", "hello2", "world2")
for (i <- a; s <- b) yield s + i

Use a multi-line for loop to flatten the following two arrays into a single array.

val a = Array(1, 2)
val b = Array("hello", "world")
// add your code here
// desired result: Array("hello1", "world1", "hello2", "world2")
for {
  i <- a
  s <- b
} yield s + i