Currying is a technique of translating a method that takes multiple arguments into a sequence of methods each with a single argument.
// uncurryied version
public static int Multiply(int x1, int x2)
{
return x1 * x2;
}
// curryied version
public static Func<int, Func<int, int>> Multiply()
{
return x1 => (x2 => x1 * x2);
}
Why?
Using the currying technique gives us a different syntax that can be more useful for building up a library of composite functions made from simpler functions. For a very (very) simple example if we wanted to create a function that doubled or tripled a number we could do the following using currying:
// uncurryied multiply
Multiple(3,2); // = 6
// curryied multiply
Multiply()(2)(3); // = 6
// with the uncurryied multiply we can now do this
var doubleFunction = Multiply()(2);
var tripleFunction = Multiply()(3);
var doubleResult = doubleFunction(3); // = 6
var tripleResult = tripleFunction(3); // = 9