std.functional.curry isn't

Graham Fawcett fawcett at uwindsor.ca
Thu Jun 24 17:06:00 PDT 2010


Hi folks,

The template "std.functional.curry(alias fun, alias arg)" claims to 
"curry fun by tying its first argument to a particular value."

That is not currying; it is partial application. In particular, it is a 
kind of partial application called a "left section," because the argument 
provided is partially applied as the left-hand argument of the binary 
function.

Partial application takes a binary function and a value, and returns a 
unary function.

Currying takes a binary function and returns a unary function, which 
returns a unary function.

Let [A,B]-> C be the type of binary functions taking arguments of types A 
and B, and returning a value of type C. A unary function from B to C can 
be written [B]->C.

left section:  [ ([A,B]->C), A ] -> ([B] -> C). 
right section: [ ([A,B]->C), B ] -> ([A] -> C). 
curry:         [ ([A,B]->C) ] -> ([A] -> ([B] -> C)).

The example in the std.functional documentation:

int fun(int a, int b) { return a + b; }
alias curry!(fun, 5) fun5;
assert(fun5(6) == 11);

If this were a real curry, you would write it like this:

int fun(int a, int b) { return a + b; }
assert(curry!(fun)(5)(6) == 11);

Confusing curring and partial application is a very common mistake. But 
please rename this template in std.functional. It makes the 
std.functional module look amateurish to have such an error.

I'd vote for "template partial(alias fun, alias arg)" and "template 
partialR(alias fun, alias arg)" for left and right sections. It's 
unlikely that you really need a "curry" template at all.

Regards,
Graham


More information about the Digitalmars-d mailing list