Re: Some questions about currying

2003-10-30 Thread Joe Gottman

- Original Message - 
From: "Luke Palmer" <[EMAIL PROTECTED]>

> Joe Gottman writes:
> > 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.
>
>
> > This would probably have to be a different method than assuming.
> > Maybe something like &printNum(int).bind(x => \$foo)

   But getting the signature of the returned code object right is a major
reason for using any kind of currying function instead of just writing a
scratchpad.  Getting the signature right is a non-trivial task, and
.assuming and .bind would have to do exactly the same work to compute the
signature.  Therefore, if we are defining one in the core I think we might
as well define the other as well.

Joe Gottman




Re: Some questions about currying

2003-10-30 Thread Jonathan Scott Duff
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]


Re: Some questions about currying

2003-10-29 Thread Luke Palmer
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
> 
> 
> 


Some questions about currying

2003-10-29 Thread Joe Gottman

   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?

   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
}

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")

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.  This would
probably have to be a different method than assuming.  Maybe something like
&printNum(int).bind(x => \$foo)


Joe Gottman