Logo

Programming-Idioms

  • JS

Idiom #202 Sum of squares

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

s = data.reduce((a, c) => a + c ** 2, 0)

Array.reduce uses a function to reduce the array into a single sum of all the elements' squares.

The initial accumulator's value is 0.
(defn square [x] (* x x))

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

New implementation...
< >
Bart