Spice Up Your C# Code With Currying

[UPDATE: When I first posted this, I used the term currying to describe partial function application. Though they are related concepts, they mean different things. Since this post is more about partial function application than currying, I have corrected the terminology below.]

C# doesn’t have language supp1ort for currying, but that doesn’t mean that you can’t spice up your code by currying or partially applying methods yourself. It’s also very easy to do:

As you can see, this simple method returns a delegate accepting one parameter corresponding to the second argument of the input function. This means that the returned delegate acts the same way as the input function, except that the first argument is now bound to the input parameter. We’ve partially applied the method.

Pretty cool. Now, there is a little bit of work to do because you have to write a new bind function for each number of arguments that you want to support. I supported up to 4 arguments, since that meshes with the definition of Func. You also will probably want to write out Bind1, Bind2, Bind3, and Bind4 methods, so that you aren’t limited to always binding to the first argument. That’s a lot of "duplicated" code for the definitions, but C# doesn’t have a way to specify variable-arity (think params[]) for generic parameters, so you’re stuck writing everything out.

[UPDATE: instead of writing these bind methods by hand, you can use Elevate or just use F#]

Another interesting thing to note is that you have to explicitly cast your method as a Func for this to work, or you’ll get a "type arguments cannot be inferred from usage" compile error. It would be nice to be able to just write:

var CurriedCreate = Bind1(CreateMessage);

but, alas, C#’s type inference doesn’t let you do this. Implementing Bind1 as an extension method is one option to clean up the syntax, though: