Perl 6 An Introduction to Perl 6 #18

Reduction operators (pairwise infix)

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

Perl 6:

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

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

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

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

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

$min = [min] @a;           # smallest value of @a
Copyright © 2008 Patrick Michaud