Skip to content

Instantly share code, notes, and snippets.

@FCO
Created September 17, 2021 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FCO/58f648cb9df26fa127519358f81d9f03 to your computer and use it in GitHub Desktop.
Save FCO/58f648cb9df26fa127519358f81d9f03 to your computer and use it in GitHub Desktop.
currying with named anywhere
sub infix:<.%>(&func, Capture \args) {
my $r = &func;
my @args = |args.Array;
my %named = |args.Hash;
my $capture;
my $start = 0;
loop {
my @named = $r.signature.params.grep(*.named).map(*.name.substr: 1);
my %pars = %named{ @named }:p:delete;
my $end = +@args;
repeat { $capture = @args[$start..--$end].Capture } until $r.cando: $capture;
$r = $r(|$capture, |%pars);
$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, 3, :2b); # 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment