Logo

Programming-Idioms

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

Idiom #202 Sum of squares

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

use List::Util qw(sum);
my $s = sum map { $_ ** 2 } @data;
(defn square [x] (* x x))

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

New implementation...
< >
Bart