documentcloud/underscore — pull request #1044 by almost (2013-03-31 16:44:25+00:00)
Title: Added a _.curry function

I've added a `_.curry` function, I think it's useful, does anyone else agree?

I see `_.curry` functions have been suggested before, but there seems to have been previous confusion of the difference between `curry` and `partial`.

From the Haskell wiki[1]:

````
Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.
```

I've added a `_.curry` function. It works slightly differently to take account of the very different nature of JavaScript vs. Haskell.

A simple example:

```
function add(a,b) {
return a+b;
}
var curriedAdd _.curry(add);
var increment = curriedAdd(1);
console.log(increment(3));
// 4
```

But `_.curry` doesn't just work with two argument functions:

```
function tripple(first, second, third) {
return [first, second, third];
}
var curriedTripple = _.curry(tripple);
var startsWithMoe = curriedTripple("moe");
console.log(startsWithMoe("larry")("curly"));
// ["moe", "larry", "curly"]
```

You can supply more than one argument with each call:

```
console.log(startsWithMoe("larry", "curly"));
// ["moe", "larry", "curly"]
```

So a curried function in this case acts the same as the original if you supply all the arguments at once:

```
console.log(curriedTripple("moe","larry", "curl"))
// ["moe", "larry", "curly"]
```

If you supply more arguments than are left they'll get passed through to the function and by available in the `arguments` variable as normal.

To support functions which don't specify their arguments explicitly and instead use the `arguments` variable you can pass in the number of arguments to expect to `_.curry`:

```
function tuple () {
return _.toArray(arguments);
}
var curriedPair = _.curry(tuple, 2);
console.log(curriedPair("moe")("larry"))
// ["moe", "larry"]
```

[1]: http://www.haskell.org/haskellwiki/Currying