Skip to content

Instantly share code, notes, and snippets.

@CIAvash
Created September 17, 2021 13:03
Show Gist options
  • Save CIAvash/686114e5b4b3dedff74c398d725fcf3b to your computer and use it in GitHub Desktop.
Save CIAvash/686114e5b4b3dedff74c398d725fcf3b to your computer and use it in GitHub Desktop.
Raku - Call returned functions from a function on arguments
#!/usr/bin/env raku
sub infix:<.%>(&func, +@args) {
my $r = &func;
my $capture;
my $start = 0;
loop {
my $end = +@args;
repeat { $capture = @args[$start..--$end].Capture } until $r.cando: $capture;
$r = $r(|$capture);
$start = $end + 1;
last if $start == @args || $r !~~ Callable;
}
$r;
}
my &func = -> $a { -> $b { $a + $b } };
my &func2 = -> $a, :$b { -> $c, $d = 0 { $a + ($b // 0) + $c + $d } };
say &func.%(1, 2); # 3
say &func2.%(1, :b(2), 3); # 6
say &func2.%(1, :b(2), 3, 4); # 10
say &func2.%(1, 3, 4); # 8
my &func3 = &func2.%(1, :b(2));
say &func3.%(5, 6); # 14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment