Course of Raku / Advanced / Operators / User-defined operators / Exercises / Average operator
Solution: Average operator
Here is a possible solution to the task.
Code
sub infix:<avg>($a, $b) {
($a + $b) / 2
}
say 4 avg 10;🦋 You can find the source code in the file average-operator.raku.
Output
7Comments
The operator is declared as
infix:<avg>, so it is written between its two operands, just like+or~.The body adds the two operands and divides by two. For
4and10the result is7.