Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

-stvn

In reply to Re^3: Near-free function currying in Perl by stvn
in thread Near-free function currying in Perl by tmoertel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found