On Wed, Oct 29, 2003 at 08:32:40PM -0500, Joe Gottman wrote:
>I just reread the section of A6 about currying, and I have several
> questions about it.
Your questions have caused me to skim over A6 and discover that there
are lots of details I've forgotten. Hopefully forgetfulness won't be
a problem when we've a perl6 to strengthen those neuronal connections.
:-)
>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?
Perhaps because I'm feeling a bit pythony today, but why would you do
that? Two parameters that differ only in sigil can hardly be
descriptive.
>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
> }
Hmm. IIRC assuming returns the function to you. Since you're not
capturing it anywhere, the above is slightly useless. It needs to be
something like:
sub setFirstToOne(&block is rw) {
&block := &block.assuming( 0 => 1);
}
Assuming that's what you meant, you can probably just do it with a
wrap (bear with me, I'm muddling through):
sub setFirstToOne(&block is rw) {
&block.wrap(sub ([EMAIL PROTECTED]) { call(1,[EMAIL PROTECTED]); });
}
Now if you didn't want to muck with &block itself, but return a new bit
of code, you can do this:
sub setFirstToOne(&block is copy) {
&block.wrap(sub ([EMAIL PROTECTED]) { call(1,[EMAIL PROTECTED]); });
return █
}
Where after the call to setFirstToOne(), &block will still have its
original semantics, but the returned subref will have the new
semantics.
> 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")
sub foo ([EMAIL PROTECTED]) { ... }
my $bar = &foo;
$bar.wrap(sub ([EMAIL PROTECTED]) { call("hello",[EMAIL PROTECTED]); });
$bar.("World");
Since wrap() does in-place manipulation, I'm not sure what that means
for &foo after you've wrapped $bar. Perhaps you need to do the
copying trick like above.
> 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.
I'm sure there's some way to do this, but I can't think of it right
off.
-Scott
--
Jonathan Scott Duff
[EMAIL PROTECTED]