By Alvin Alexander. Last updated: November 24, 2014
I just read an article about a “sum of the squares” algorithm, and thought for a moment how I’d write it in Scala. As usual, it’s pretty easy:
List(2,3,5).map(Math.pow(_, 2)).sum
You can also use Scala’s fold or reduce methods, but for this algorithm, using sum
is easiest.