11

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.

Pat
  • 36,282
  • 18
  • 72
  • 87
brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • 1
    FWIW, there is *some* EVAL underneath at the moment, after extensive sanity checking. But if you make sure this happens at compile time, e.g. by prefixing BEGIN, like: `my &smith = BEGIN &first-and-last.assuming( Empty, 'Smith' );` you would not suffer any de-optimization effects at runtime. – Elizabeth Mattijsen Apr 28 '17 at 11:42

1 Answers1

12

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.');
brian d foy
  • 129,424
  • 31
  • 207
  • 592