Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Clojure

Idiom #202 Sum of squares

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

(defn square [x] (* x x))

(def s (reduce + (map square data)))
(defn square [x] (* x x))

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

This doesn't create any intermediate collection
(defn square [x] (* x x))

(def s (->> data (map square) (reduce +)))
using System.Linq;
var s = data.Sum(x => x * x);

New implementation...
< >
Bart