Course of Raku / Advanced / Operators / Meta-operators
Reduction metaoperators
A reduction meta-operator is written as a regular operator wrapped in
square brackets, such as [+] or [*]. It takes
a list of values and inserts the operator between every pair of them,
reducing the whole list to a single value.
For example, [+] adds up all the elements of a list:
my @data = 3, 5, 7, 9, 11;
say [+] @data; # 35The construct [+] @data is equivalent to writing the
operator out by hand:
say 3 + 5 + 7 + 9 + 11; # 35Any suitable infix operator works the same way. With [*]
you get the product of the list, so applying it to the range
1..$n is a handy way to compute a factorial:
my $n = 5;
say [*] 1..$n; # 120Here, the range 1..$n produces the numbers from 1 to 5,
and [*] multiplies them: 1 * 2 * 3 * 4 * 5,
which is 5!.
String concatenation works too. The [~] reduction joins
a list of strings into one:
my @strings = <neun hundert fünf und zwanzig>;
say [~] @strings; # neunhundertfünfundzwanzigEven comparison operators can be reduced. [<] reports
whether the values are in strictly increasing order:
say [<] 1, 2, 3; # TrueTriangular reduction
If you put a backslash inside the brackets, you get a triangular reduction, which keeps all the intermediate results instead of only the final one:
say [\+] 1..5; # (1 3 6 10 15)Each element of the result is a partial sum: 1, then
1+2, then 1+2+3, and so on up to the sum of
the whole list.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Meta-operators | Quiz — Reduction →
💪 Or jump directly to the exercises in this
section.