Named curies

One of the challenges of using curries is that the usual implementation of a curry looks like a function call and not a function reference.

For example:

1
greet('bob');

Did the above greet Bob, or did it return a greeting function that is now customized for Bob?

1
2
3
4
5
function greet(name) {
return function(greeting) {
alert(greeting + ', ' + name);
}
}

Of course we can improve our naming:

1
getFunctionReferenceToGreet('bob')

The problem with the above is that JavaScript has trained us to expect parentheses to be mean function calls, and the absence of parentheses to mean function references. The above curry violates this. It is the same reason that reading codes with many bind(this) calls is slow going.

A solution is to make curries conform to the convention. Creating the curry is a function call, and the curry returns a function reference:

1
createGreetingFor('bob').greet

Using the above, you can still scan a file and quickly pick out the function calls and function references without having to read the function names.

The above would be implemented as follows:

1
2
3
4
5
6
7
function createGreetingFor(name) {
return {
greet : function(greeting) {
alert(greeting + ', ' + name);
}
};
}

and it could be used like this:

1
2
3
4
5
6
7
8
9
10
// variable assignment
var greet = createGreetingFor('bob').greet;
greet('good evening');

// callback that looks like a callback
goodMorning(createGreetingFor('bob').greet)

function goodMorning(greet) {
greet('good morning');
}