The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

FP::Currying

SYNOPSIS

    use FP::Currying;
    # see usage below

FUNCTIONS

curry(f)->(arg1)->(arg2)

Takes a function (coderef) f that takes 2 arguments, and returns a function that takes just 1 argument, which when called returns a function that takes again 1 argument and when called calls f with the two separately passed arguments.

    use FP::Array 'array';

    is_deeply curry(\&array)->(10)->(20),
              [ 10, 20 ];
curry_(f, args..)->(args1..)->(args2..)

Same as curry but accepts multiple arguments in each step.

    is_deeply curry_(\&array, 1)->(10, 11)->(20, 21, 23),
              [ 1, 10, 11, 20, 21, 23 ];
partial(f, args...)->(args1..)

Takes a function f and fewer than the normal arguments to f, and returns a fn that takes a variable number of additional args. When called, the returned function calls f with args + additional args.

(Same as curry_ but with only one step.)

    is_deeply partial(\&array, "hi", 1)->(3, 9),
              [ "hi", 1, 3, 9 ];
uncurry, uncurry_1_1, uncurry_2_1, uncurry_1_2, uncurry_2_2, uncurry_1_1_1

Sometimes it's easier to write code in a curried fashion. Often users still expect to receive an uncurried ("normal") version of the function. `uncurry_1_1 $fn` returns a function that expects 2 arguments, passes the first to $fn and then the second to the function that $fn returns. Other variants behave similarly: the appendix tells how many arguments each function level expects; the added numbers determine how many arguments the resulting function expects.

    my $mult = uncurry sub { my ($x) = @_; sub { my ($y) = @_; $x*$y }};
    is &$mult(2,3), 6;
    # 'uncurry' is an alias to 'uncurry_1_1'.

    my $mult3 = uncurry_2_1 sub { my ($x,$y) = @_; sub { my ($z) = @_; $x*$y*$z }};
    is &$mult3(2,3,4), 24;

TODO

Add tail-call optimization to the last call in the chain. Waiting till Sub::Call::Tail is fixed, or better, we've got a switchable variant.

SEE ALSO

There are also various modules for currying (curry, not uncurry) on CPAN.

`the_method` and `cut_method` in FP::Ops.

NOTE

This is alpha software! Read the status section in the package README or on the website.