loading...

re: Daily Challenge #225 - Square'n'Sum VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Ruby: def square_sum(arr) arr.sum { |n| n * n } end
 

A GolfScript solution:

{{. *}%{+}*}:ssq;

[1 2 2]ssq
#=> 14

Explanation: map (%) the block {. *} over the input. . duplicates the element, * multiplies the top 2 stack elements. Then fold (*) a block performing addition {+} over the result.

code of conduct - report abuse