curry

#!/usr/bin/env perl
# Time-stamp: <2007-08-02 13:36:56 JST, hirose31>

use strict;
use warnings;
use Test::Base;

sub curry {
    my($func, @argv) = @_;
    sub {
        &$func(@argv, @_);
    }
}
sub add {
    return $_[0]+$_[1];
}
sub sum {
    my $sum;
    $sum += $_ for @_;
    return $sum;
}

filters {
    input => ['split=,'],
};

plan tests => 5 * blocks;

my $add3         = curry(\&add, 3);
my $sum_plus_5   = curry(\&sum, 5);
my $sum_plus_2_4 = curry(\&sum, 2, 4);

run {
    my $block = shift;
    my @argv = $block->input;

    is add(@argv),             $block->add,          $block->name.' add';
    is $add3->($argv[0]),      $block->add3,         $block->name.' add3';

    is sum(@argv),             $block->sum,          $block->name.' sum';
    is $sum_plus_5->(@argv),   $block->sum_plus_5,   $block->name.' sum+5';
    is $sum_plus_2_4->(@argv), $block->sum_plus_2_4, $block->name.' sum+2+4';
};

__END__
=== curry2
--- input: 3,1

--- add:  4
--- add3: 6

--- sum:           4
--- sum_plus_5:    9
--- sum_plus_2_4: 10

=== curry3
--- input: 3,4,5

--- add:  7
--- add3: 6

--- sum:          12
--- sum_plus_5:   17
--- sum_plus_2_4: 18

これからカレーたべるぉ