Scala: A “sum of the squares” algorithm using map/sum and foldLeft

As a quick Scala fold/reduce example, I just needed to write a “sum of the squares” algorithm for a “Pearson Correlation” function I’m working on, and initially wrote it like this using map and sum:

val sumOfTheSquares = movieRatingsMap.values
                                     .map(rating => Math.pow(rating, 2))
                                     .sum

If you know Scala, and know that movieRatingsMap is a Map of movies and my ratings of those movies, I think that line of code is fairly easy to read. That line can also be written like this:

val sumOfTheSquares = movieRatingsMap.values.map(Math.pow(_, 2)).sum

though even after using Scala for several years, I still find that line to be a little less readable.

Using ‘foldLeft’ instead

As I thought about this a little more, I realized that this could be a good place to use a reduce or fold algorithm. You can’t really use the reduce family of methods for this algorithm, but you can use foldLeft, and that code looks like this:

val sumOfTheSquares = movieRatingsMap.values.foldLeft(0.0)(_ + Math.pow(_, 2))

Personally I think that line of code is harder to read, but it may be more efficient than the map/sum approach, because the map/sum solution requires an intermediate collection, while foldLeft presumably just creates a simple sum of the squares as it walks through each value in the Map.

As an interesting quiz, do you know why you can’t use reduce with this algorithm?

Summary

In summary, this code shows a few different ways to create a “sum of the squares” algorithm in Scala. I’m sure there are many more ways to do this, but these are the first two approaches that came to mind.

Which reminds me of a Viktor Klang proverb: “If you only have one solution, it may well be the worst.”