Learn Scala - Map, Fold, Reduce

Question Click to View Answer

What does the following code print?

var stringNums = List("4", "10", "13")
println {
  stringNums.map( (n: String) => n.toInt )
}

List(4, 10, 13) is printed.

The map function is used to convert all of the strings in the list into integers.

Cube every element in the following list.

var goats = List(23, 99)

The map function is used to cube every element in the goats list. The scala.math.pow function is used to actually cube the number.

var goats = List(23, 99)
println {
  goats.map { (num) =>
    scala.math.pow(num, 3)
  }
}

What does the following code print?

var odds = List(1, 5, 7)
println {
  odds.foldLeft(0) { (memo: Int, num: Int) =>
    memo + num
  }
}

13 is printed.

The foldLeft function is initialized with a starting value of zero and the running sum is accumulated in the memo variable. This code sums all the numbers in the odds list.

Find the longest string in the following array.

var names = List("tuco", "jesse", "hank", "gustavo")

The reduceLeft function is used to compare every pair of strings in the list.

println {
  names.reduceLeft { (s1: String, s2: String) =>
    if (s1.length > s2.length) s1 else s2
  }
}

The loop starts by comparing tuco with jesse. jesse is longer, so jesse is compared with the next element, hank. jesse is still longer, so jesse is compared with gustavo. gustavo is longer than jesse and there are no more elements in the list, so gustavo is returned.

Concatenate the strings in the following list using the foldLeft function.

var letters = List("g", "o", "a", "t")

The foldLeft function is initialized with an empty string and the function iterates over all the letters in the list. Each letter is concatenated with the memo during each step of the iteration.

println {
  letters.foldLeft("") { (memo: String, letter: String) =>
    memo + letter
  }
}

Loop through the following list from right to left and concatenate all of the letters. The answer should be cool

var huh = List("l", "o", "o", "c")

The foldRight function iterates through a list from right to left. The argument order is swapped in the anonymous function - the letter comes first, followed by the accumulator variable that we name memo

println {
  huh.foldRight("") { (letter: String, memo: String) =>
    memo + letter
  }
}

Use the reduceLeft function with the following list to create a string that is separated by commas (the result should be "brazil, argentina, colombia").

var countries = List("brazil", "argentina", "colombia")

The reduceLeft function is very similar to foldLeft, but it doesn't need to be initialized with a starting value. We can simply state the type of the output - a string in this case.

println {
  countries.reduceLeft[String] { (c1: String, c2: String) =>
    s"$c1, $c2"
  }
}

Return an array that uppercases all of the letters in the following list.

var me = List("i", "am", "quiet")

The map function can be used to easily create a new list from an existing list.

println {
  me.map { (word: String) =>
    word.toUpperCase
  }
}

Sum all of the even numbers in the following list.

var whatever = List(1, 2, 3, 4, 5, 6, 7)

The foldLeft function can be used in conjunction with an if statement to sum all of the even numbers in a list.

println {
  whatever.foldLeft(0) { (memo: Int, num: Int) =>
    if (num % 2 == 0) memo + num else memo
  }
}

Concatenate all the words in the following list that start with the letter "b". Seperate each word with a space.

var rrr = List("ant", "beer", "battered", "cool", "burger")
rrr.filter { (w: String) =>
  w.take(1) == "b"
}.reduceLeft { (a: String, b: String) =>
  s"$a $b"
}