The methods withDefault
and withDefaultValue
do not change the map. Instead, they simply return a default value. Let's remove the syntactic sugar form your statement to see where it goes wrong:
scores(char)(i) += nGram._2
scores(char)(i) = scores(char)(i) + nGram._2
scores.apply(char)(i) = scores.apply(char)(i) + nGram._2
scores.apply(char).update(i, scores.apply(char).apply(i) + nGram._2)
Now, since scores.apply(char)
does not exist, a default is being returned, Map[Int, Double]().withDefaultValue(0.0)
, and that map gets modified. Unfortunately, it never gets assigned to scores
, because no update
method is called on it. Try this code below -- it's untested, but it shouldn't be hard to get it to work:
scores(char) = scores(char) // initializes the map for that key, if it doesn't exist
scores(char)(i) += nGram._2