Programming-Idioms

New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
s = sum( data**2 )
sumOfSquares = sum . map (^2)
s = data.reduce((a, c) => a + c ** 2, 0)
import java.util.Arrays;
Arrays.stream(data).map(i -> i * i).sum()
math
var
  data: array of double;
...
  s := SumOfSquares(data);
...
use List::Util qw(sum);
my $s = sum map { $_ ** 2 } @data;
s = sum(i**2 for i in data)
s = data.sum{|i| i**2}
let s = data.iter().map(|x| x.powi(2)).sum::<f32>();