LOL, you beat me by only a few minutes, I was just going to posts this.
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 3;
{
package Test;
use Attribute::Handlers;
sub curry : ATTR(CODE) {
my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
my @args = split(//, $data);
my $num_args = scalar(@args);
my $func = *{$symbol}{NAME};
no strict 'refs';
no warnings 'redefine';
*{"${package}::${func}"} = sub {
if (scalar(@_) == $num_args) {
goto $referent;
}
else {
my @args = @_;
return sub {
$referent->(@args, @_);
};
}
};
}
sub foo : curry('$$$') {
return @_;
}
}
is_deeply(
[ Test::foo(1, 2, 3) ],
[ 1, 2, 3 ],
'... got the right return value');
my $curried_foo = Test::foo(1, 2);
is(ref($curried_foo), 'CODE', '... this is our curried sub');
is_deeply(
[ $curried_foo->(3) ],
[ 1, 2, 3 ],
'... got the right return value now');
1;
Although to be honest, neither of our implementations, nor Tom's do what Haskell and Standard ML do, which is too keep currying until all the functions arguments are satisfied.
my $curried_foo = foo(1);
my $even_more_curried_foo = $curried_foo->(2);
print $even_more_curried_foo->(3); # now we execute the function
And from my (limited) understanding of prototypes, it seems that this may not be possible since seems it is difficult to assign an attribute to a closure.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|