First-class functions

Here we use the Z ("zipwith") metaoperator to zip the š“ and šµ lists with builtin composition operator, āˆ˜ (or just o). The .() construct invokes the function contained in the $_ (current topic) variable.

my \š“ = &sin,  &cos,  { $_ ** <3/1> }
my \šµ = &asin, &acos, { $_ ** <1/3> }

say .(.5) for š“ Zāˆ˜ šµ

Output:

0.5
0.4999999999999999
0.5000000000000001

It is not clear why we don't get exactly 0.5, here.

Operators, both builtin and user-defined, are first class too.

my @a = 1,2,3;
my @op = &infix:<+>, &infix:<->, &infix:<*>;
for flat @a Z @op -> $v, &op { say 42.&op($v) }

Output:

43
40
126

Last updated