What is the advantage of Currying in C#?
What is the advantage of achieving partial function application on a curried function?
I've found partial function application, as opposed to currying, to be useful when I want to reuse code.
To be clear, since the definitions of currying and partial function application seem to get blurred, by partial function application I mean taking a function with N parameters, and converting it into a function with N-1 parameters.
In particular, it's been handy when writing unit tests. Since I'll be writing hundreds of unit tests I try to reuse test code wherever possible. So I may have a common test method that takes a delegate to a method I wish to test, plus some parameters to that method and an expected result. The common test method will execute the method under test with the supplied parameters, and will have several assertions comparing the result to the expected result.
The problem comes when I want to test a method that has more parameters than the delegate being passed into the common test method. I could write another common test method which is identical to the first one, apart from taking a delegate with a different signature. That seems like repeating myself, however. To avoid having to write such duplicate code, I can use partial function application to convert a delegate taking, say, two parameters, into a delegate taking a single parameter. Now I can use my common test method to test methods that take either one or two parameters.
Here's one of the helper methods I use to fix one of the arguments of the delegate that was passed in:
///
/// Fixes an argument of an action delegate, creating a closure that combines the
/// delegate and the argument value.
///
/// An action delegate which takes only one argument.
public static Action FixActionArgument(Action action,
TIn2 argumentValue)
{
return in1 => action(in1, argumentValue);
}