Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Perl 6 assume a positional parameter that's not the first one?

Tags:

currying

raku

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.

like image 500
brian d foy Avatar asked Apr 27 '17 23:04

brian d foy


1 Answers

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.');
like image 59
brian d foy Avatar answered Sep 21 '22 15:09

brian d foy