Logo

Programming-Idioms

  • Python
  • Scala

Idiom #202 Sum of squares

Calculate the sum of squares s of data, an array of floating point values.

f = lambda x: x * x
s = sum(map(f, data))
s = sum(i**2 for i in data)
(defn square [x] (* x x))

(def s (reduce + (map square data)))

New implementation...
< >
Bart