Currying
My recipe for curry is black pepper, bay leaves, cumin, turmeric, paprika, coriander, and garlic—but I’m not talking about that kind of curry.
Currying in Lambda expressions is sort of like a Turing machine for Lambda expressions. Currying simply means breaking up a Lambda expression so that it accepts up to one input and has up to one output. Each input and expression calculates one part of the solution, and the output from that Lambda expression is an input to the next Lambda expression in the chain. When you think of currying, think of a chain of one-input/one-step Lambda expressions, each of which is part of a whole solution.
Using arithmetic to demonstrate, suppose you want to add three numbers—two input numbers and a third fixed number. A Lambda expression to add the three numbers could be written like this:
(x,y) => x + y + 1;
The Lambda expression above could be assigned to a generic delegate Func that has three integer-parameterized arguments. For example:
Func<int, int, int> lambda = (x,y) => x + y + 1;
The delegate Lambda is called like a function; for example, lambda(2,3) yields 6.
A curried version would use two Lambda expressions, each accepting one input, and might be written like this:
X => y => x + y + 1;
The signature for this curried Lambda expression clearly indicates a chain of Lambda expressions:
Func<int, Func<int, int>> curry = x => y => x + y + 1;
This delegate—representing the Lambda expression—is called like this:
curry(2)(3)
That’s because curry(2) returns the Lambda expression y => x + y + 1 with x fixed. This is implicitly an additional function, hence the (2) function call notation.
The indicator that you’re dealing with currying is the presence of the nested generic delegates in the parameter types, or the presence of multiple => tokens in a Lambda expression.