๐Ÿ“˜ Reverse meta-operator R in Perl 6

๐Ÿ“˜ Reverse meta-operator R in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

The prefix R forms the reverse operator for the infix operators, such as / or cmp. The reverse operator does the same as the original but changes the order of the operands.

If necessary, it also changes the operatorโ€™s associativity. This matters when you have more than two operands in a row. For example, in the code $a op $b $op $c the operators are calculated left to right; that is, first, the value of $a is evaluated. With the reverse operator, the value of $c will be calculated first in the same sequence $a Rop $b Rop $c.

say 2 R/ 10; # 5. Same as say 10 / 2

The reverse operators may be very useful together with the reduction operators. Here is an example of how you can reverse and join the values in one go:

say [R~] 'a'..'z'; # zyxwvutsrqponmlkjihgfedcba

Leave a Reply

Your email address will not be published. Required fields are marked *