Perl 6 Perl 6 today #51

Reduction operators (pairwise infix)

Any infix operator can be surrounded by brackets to turn it into a "reducing" list operator:

[+] 1, 2, 3                # 1 + 2 + 3 == 6

$sum = [+] @a;             # sum all elements of @a

$prod = [*] @a;            # multiply all elements of @a

$mean = ([+] @a) / @a;     # calculate mean of @a

[*] 1..$n                  # $n factorial

[<] @a                     # true if @a[0] < @a[1] < @a[2] < ...

$sumsq = [+] (@x »**» 2);  # sum of squares of @x

$mean = ([+] @x) / @x;     # mean of @x
$stddev = sqrt( ([+] (@x »-» $mean) ) »*»  2 / @x );
Copyright © 2009
http://www.pmichaud.com/2009/pres/