Created
September 17, 2021 13:03
-
-
Save CIAvash/686114e5b4b3dedff74c398d725fcf3b to your computer and use it in GitHub Desktop.
Raku - Call returned functions from a function on arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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