Joe Gottman writes:
>I just reread the section of A6 about currying, and I have several
> questions about it.
>
>1) Suppose I have a function like the following:
>sub foo($param, @param) {...}
> where the parameter names differ only by their sigils. Is it legal
> for me to type
> &foo.assuming('$param' => 1)
> in order to disambiguate the parameter names?
Yeah, but I think the scalar would get it anyway. This is the same way
$0 works. So 'foo' and '$foo' refer to the same thing, and '@foo' and
'%foo' disambiguate. Though, if $param weren't there, 'param' => [1]
would assign to @param... which is not, IIRC, how $0 works. I think
it'd be a good idea to unify the two behaviors, probably in favor of the
named parameter style (where it will pick the defined one, and the
scalar only if it's ambiguous).
>2) Often, if you are working with a code reference instead of an actual
> sub, you might not know the names of all the parameters. In this case,
> would it be legal to curry by parameter position instead of parameter name?
>
> sub setFirstToOne(&block) {
> &block.assuming( 0 => 1); #I am assuming 0-based parameter lists
> }
That would definitely be a nice feature.
> Another advantage of currying by position is that it might allow you to set
> the first few elements of a slurpy array.
>
> sub foo(* @params) {...}
> my $bar = foo.assuming(0 => "hello");
> $bar.("world"); # Calls foo("hello", "world")
That would require a bit more under-the-hood work, but I think that's
okay.
> 3) Currying binds a function parameter to a value? Is there any way to
> bind a function parameter to a variable? Consider the following code:
>
> sub printNum(int $x) {print "$x\n";}
> my $foo = 0;
> my $vindaloo = &printNum(int).assuming(x => $foo); #currying
> ++$foo;
> $vindaloo.();
>
>This code prints 0, not 1, because the currying binds the parameter to
> the value $foo had when the currying occurred, not the value it had when the
> curried function was called. It would be nice if there were some way to
> curry so that a parameter is bound to a variable reference.
There is. Best not to worry .assuming with details of references. Do
it with a scratchpad:
sub printNum($x) { print "$x\n" }
my $foo = 0;
my $vindaloo = { printNum($foo) };
++$foo;
$vindaloo(); # Prints 1
There's still those issues of getting $vindaloo's signature exactly
right for complex cases. In any case, I don't think .bind would be hard
to write.
Luke
> This would probably have to be a different method than assuming.
> Maybe something like &printNum(int).bind(x => \$foo)
>
>
> Joe Gottman
>
>
>