Map with map

Scrabble Score
Scrabble Score in Scala
object ScrabbleScore {

  private val lookup: Map[Char, Int] = Map(
    1 -> "AEIOULNRST",
    2 -> "DG",
    3 -> "BCMP",
    4 -> "FHVWY",
    5 -> "K",
    8 -> "JX",
    10 -> "QZ"
  ).flatMap(tuple => tuple._2.map((_, tuple._1)))

  def score(word: String): Int = word.toUpperCase.map(lookup(_)).sum
}

This approach starts be defining a private Map for associating a score with a String of letters. To reduce some tedious typing, the flatMap() and map() methods are used to transform the Map that looks like

8 -> 'JX', 10 -> 'QZ'

to one that looks like

'J' -> 8, 'X' -> 8, 'Q' -> 10, 'Z' -> 10.

Each key/value entry in the Map is passed as a tuple to the lambda in flatMap(), in which each character in the value String is passed to the map() method. Inside the map() method, a new tuple is created, with the character for the key and the score for the value.

The score() method passes each uppercased character to the map() method, which passes each character to the lookup Map, which returns the score for that character.

The score() method returns the result of passing all of the scores to the sum() method.

9th Oct 2024 · Found it useful?