Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Higher-order functions / Exercises / Make a multiplier
Solution: Make a multiplier
Here is a possible solution to the task.
Code
sub multiplier($n) {
sub ($x) { $x * $n };
}
my &double = multiplier(2);
say double(7);🦋 You can find the source code in the file make-adder.raku.
Output
14Comments
multiplier(2)returns a subroutine that remembers$nis2.Calling it with
7multiplies the two, giving14. A different argument tomultiplier— say3— would produce a different multiplying function.