A neat way to curry in C#

May 25, 2018 c#, fp

I was reading an older blogpost from Mike Hadlow about Partial Application in C# in which he discusses how Partial Application can be implemented in C# via Currying. Although I appreciate his example of implementing currying via extsion methods, the syntax is hideous. There is a suggestion in the comments though that I found to be a much better solution.

// Define a local function Add. int Add(int a, int b) => a + b; // Here we do the currying. Func<int, int> add3 = (b) => Add(3, b); // This will print 5. Console.WriteLine(add3(2)); // Curry one more time so that we have // a function that simply produces 5. Func<int> five = () => add3(2); // This will also print 5. Console.WriteLine(five());