Logo

Programming-Idioms

History of Idiom 202 > diff from v12 to v13

Edit summary for version 13 by misha:
New Clojure implementation by user [misha]

Version 12

2020-03-17, 13:20:52

Version 13

2020-04-29, 12:10:45

Idiom #202 Sum of squares

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

Idiom #202 Sum of squares

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

Code
(def numbers [1 2 3.14 4 -5 6/7 7N 8M])
(defn square [x] (* x x))

(reduce + (map square numbers))

;or with syntax sugar:
(->> numbers (map square) (reduce +))

;or without creating intermediate collection:
(transduce (map square) + numbers)

;or inline:
(reduce + (map #(* % %) [1 2 3]))
Doc URL
https://clojuredocs.org/clojure.core/-%3E%3E