Perl 6 allows you to curry subroutines with .assuming. That's easy to do when you want to assume the leading positional parameters:
# The first one
{
sub first-and-last ( $first, $last ) {
say "Name is $first $last";
}
my &joe = &first-and-last.assuming( 'Joe' );
&joe.( 'Jones' );
}
But what if I want to assume one of the other positional parameters while leaving the first ones alone? How can I tell .assuming
which parameters to assume?
# The second one
try {
sub first-and-last ( $first, $last ) {
say "Name is $first $last";
}
my &smith = &first-and-last.assuming( Empty, 'Smith' );
&smith.( 'Joe' );
}
With named parameters this is straightforward, but that's not what I'm curious about.
If this is really just an EVAL underneath, that's kinda disappointing.
Huh, a Whatever works:
sub first-and-last ( $first, $last ) {
say "Name is $first $last";
}
my &smith = &first-and-last.assuming( *, 'Smith' );
&smith.( 'Joe' );
And you can handle middle parameters:
sub longer-names ( $first, $middle, $last, $suffix ) {
say "Name is $first $middle $last $suffix";
}
my &smith = &longer-names.assuming( *, *, 'Public', * );
&smith.( 'Joe', 'Q.', 'Jr.');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With