Logo

Programming-Idioms

  • Go
  • Ruby

Idiom #202 Sum of squares

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

s = data.sum{|i| i**2}
s = data.sum{ _1**2 }

Since Ruby 2.7
import "math"
var s float64
for _, d := range data {
	s += math.Pow(d, 2)
}
(defn square [x] (* x x))

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

New implementation...
< >
Bart