Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Lambdas and closures / Exercises / A multiplier

Solution: A multiplier

Here is a possible solution to the task.

Code

sub multiplier($factor) {
    -> $x { $x * $factor };
}

my &triple = multiplier(3);
say triple(4);

🦋 You can find the source code in the file multiplier.raku.

Output

12

Comments

  1. The returned pointy block closes over $factor, remembering it is 3.

  2. Calling it with 4 gives 12.

Course navigation

A multiplier   |   An accumulator