A common way in Perl to simulate currying is as follows:
I am guessing the same in a FP language might look like:sub greeting { my ($verb, $object) = @_; my $sub = defined $object ? sub { print "$verb, $object\n" } : sub { print "$verb, ", shift(), "\n" }; return $sub } my $hello = greeting('Happy Birthday', 'L~R'); $hello->(); my $bye = greeting('goodbye'); $bye->('Cruel World'); __END__ Happy Birthday, L~R goodbye, Cruel World
How does it know that I am finishing off one and not starting another? Or if the 3rd time I called greeting() I did it with two arguments - what then?greeting('Hello', 'Good Looking'); # prints immediately greeting('Woah'); # Waits ... greeting('Nelly'); # prints now that it has both arguments
The next question is with your statement:
We can't quite make it all the way because functions in Perl can accept varying numbers of arguments, and thus it's hard for us to determine reliably when currying is implied by analyzing function calls.
Prototypes, as evil as they are, do allow for optional arguments or you could always count @_. I don't see why this is a problem.
My final question for now is an apparent flaw in the design. Currying, as I understand it, only works if I don't have the last x arguments. What if I have the middle argument and the entire function requires 5? In Perl, that would be easy because I would use a hash ref and "name" my arguments. I don't see how currying helps in this situation (real currying, not Perl's substitute).
Thanks by the way - I am impressed.Cheers - L~R
Update: Added a clarification to distinguish between FP currying and the simulation of currying in Perl
In reply to Re: Near-free function currying in Perl
by Limbic~Region
in thread Near-free function currying in Perl
by tmoertel
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |