Perl 6 Cool Perl 6 #61

Reduction operators (pairwise infix)

Infix operators can be surrounded by brackets to turn them into a "reducing" list operators:

[+] 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
Copyright © 2010
http://www.pmichaud.com/2010/pres/