Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Reduction
The reduce metaoperator
Wrap any infix operator in square brackets and it becomes a reduction: it is inserted between every element of a list, collapsing the list to one value.
say [+] 1, 2, 3, 4, 5; # 15
say [*] 1, 2, 3, 4, 5; # 120[+] sums the list as if you had written
1 + 2 + 3 + 4 + 5; [*] multiplies it. The same
works with any infix operator:
say [max] 4, 9, 2, 7; # 9
say [min] 4, 9, 2, 7; # 2
say [~] 'a', 'b', 'c'; # abc[max] reduces with the max operator to find
the largest element, and [~] reduces with the
string-concatenation operator ~ to glue the pieces
together.
The reduce metaoperator works on any list, including a range:
say [+] 1..100; # 5050It is one of Raku’s most compact and expressive features: a whole loop’s worth of accumulation in a pair of brackets.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Reduction | Quiz — The reduce metaoperator →
💪 Or jump directly to the exercises in this
section.