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

Lambdas

A lambda is a function with no name. You have already met two ways to write one. The first is the pointy block, which lists its parameters after the arrow ->:

my &square = -> $x { $x * $x };
say square(7); # 49

The second is the Whatever form, where a * stands in for the argument and the expression around it becomes a one-argument function:

my &square = * ** 2;
say square(7); # 49

Both create the same kind of thing: a piece of code you can store, pass, and call. Lambdas shine as arguments to higher-order functions, where naming them would only get in the way:

say (1..5).map(-> $n { $n * $n }); # (1 4 9 16 25)
say (1..5).map(* ** 2);            # (1 4 9 16 25)

The pointy form is clearer when the body is longer or takes several parameters; the Whatever form is wonderfully short for simple expressions. They are two spellings of the same idea: a function without a name.

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Lambdas and closures   |   Quiz — Lambdas


💪 Or jump directly to the exercises in this section.