So what is it? It’s basically a function factory that customises the returned function via the passed arguments to the main function. Here’s an example:
Here i’ve passed argument values during the factory calls that are going to be used in the returned function’s calculations when itself will be called. These initial argument values are held as state in the returned function because of Closure.1 This way of returning customised functions negates the need to pass large amounts of argument data in a function call and also negates the need to create objects to hold state. It’s incredibly simple and incredibly useful for priming functions with known data.
Before it start to describe currying in any detail, i’ll describe its etymology. Currying is a rediscovery of an earlier technique discovered by a guy named Moses Schönfinkel and in many cases it is referred to as Schönfinkeling! I guess because Moses’ last name is hard to pronounce people tend to use the name of its re-discoverer Haskell Curry. For the sake of brevity i’ll use the term Currying.
Earlier I wrote that currying is the more serious cousin to partial application purely because it’s used more in true Functional Languages and for more mathematical applications such as Lambda Calculus. In principle however the two are very much alike and are frequently confused.
Currying is very similar to partial application but is a process that transforms a function to act differently regarding the data it consumes through it’s arguments. Once curried, the new function returns another function with a reduced amount of parameters with state held in closure. The difference from partial application is that each returned function will only everhave one parameter. Each invocation of the returned function will return another function that requires one parameter until the operation is complete and a value is returned.
Once a function has been curried, it is effectively ‘primed’ for partial application, because as soon as you pass an argument into a curried function, you are partially applying that argument. Unlike partial application, however, a curried function will keep returning curried functions until all arguments of the original function have been specified.
This sounds like an awesome construct and something that would be very useful, but in reality i’ve had a hard time even finding any practical application of currying anywhere on the internet. Currying is an advanced technique which is more at home in theoretical computer science than in practice.2