Attribute::Curried -- Functional goodness for Perl.
use Attribute::Curried; sub bracket :Curry(3) { $_[1].$_[0].$_[2] } sub flip :Curry(3) { &{$_[0]}(@_[2,1]); } my @xs = map { bracket $_ } 1..3; my $i = 0; my @ys = map { ++$i == 2 ? $_ : flip $_ } @xs; print join(', ', map { &$_('<', '>') } @ys), "\n"; # prints '>1<, <2>, >3<'
Currying is a powerful technique familiar to programmers in functional languages like Lisp and Haskell. When called with less arguments than it needs, a curried function will return a new function that "remembers" the arguments passed so far, i.e. a closure. Once the function has enough arguments, it will perform its operation and return a value.
The typical Scheme example is something like this:
(define add (lambda (a) (lambda (b) (+ a b)))) (define add2 (add 2)) (map add2 (list 1 2 3)) ;; => (list 3 4 5)
Using Attribute::Curried, the Perl equivalent looks like this:
Attribute::Curried
sub add :Curry(2) { $_[0] + $_[1] } *add2 = add(2); map { add2($_) } 1..3; # => (3, 4, 5)
Sean O'Rourke, <seano@cpan.org>
Bug reports welcome, patches even more welcome.
Copyright (C) 2002, 2009 Sean O'Rourke. All rights reserved, some wrongs reversed. This module is distributed under the same terms as Perl itself. Let me know if you actually find it useful.
To install Attribute::Curried, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Attribute::Curried
CPAN shell
perl -MCPAN -e shell install Attribute::Curried
For more information on module installation, please visit the detailed CPAN module installation guide.