Thursday, October 29, 2009

Functional .Net : Currying

Currying is another functional technique that is possible to achieve with C#. The technique basically allows the rewriting of a function that takes in multiple arguments to one that takes in one argument and returns a function which may in turn take more arguments, the basic premise being able to build up composite function by splitting functions down and reducing the number of parameters dealt with. I will be honest and say that  I have found the language you use (C#, F#, Haskell etc) would be more influential to your predisposition  in using this technique, as it is with many of the function patterns and IMO C# does not lend itself nearly as well as F# for example. That being said it still can be done so lets look at a basic example. For starters currying is something that is not catered for explicitly out the box in C# but can easily be done using extension methods eg:

public static Func<TArg1, Func<TArg2, TResult>> Curry<TArg1, TArg2, TResult>(this Func<TArg1, TArg2, TResult> func)
{
return a1 => a2 => func(a1, a2);
}
public static Func<TArg1, Action<TArg2>> Curry<TArg1, TArg2>(this Action<TArg1, TArg2> action)
{
return a1 => a2 => action(a1, a2);
}


These extension method now allow you to take a 2 parameter delegate and split it into a one parameter argument return an Action or Func that take one argument. This can obviously be extended and helps facilitate the separation and composition of functions.



Now from my understanding of currying it is a specific form of partial application, being that currying splits its functions down to single argument delegates while partial application make no such claim, i.e. a three argument function be be reduced to a single argument function returning a 2 argument delegate. As this is purely academia I don't really care, the principle is the same.



A trivial example of currying (I'm lazy I stole it from Matt P and it is using the extension method above) :



Func<int, int, int> multiply = (x, y) => x * y;
var curriedMultiply = multiply.Curry();
var curriedMultiplyThree = curriedMultiply(3);
var curriedMultiplyResult = curriedMultiplyThree(15);
Console.WriteLine("Result of 3 * 15 = {0}", curriedMultiplyResult);


Unfortunately the verbosity of C# when approaching this style of coding very quickly begins to put me off.  The equivalent in F# is much more readable, but hey its what the language is strong at so it really should be a nicer experience. Either way its good to know the facilities are there if one day I do ever need to use it.



Basically the take away from this post is the extension methods at the top, with out these there will be no curry love.



Links forwarding (yeah this was a lazy post):



http://codebetter.com/blogs/matthew.podwysocki/archive/2009/04/26/functional-c-forward-functional-composition.aspx



http://mikehadlow.blogspot.com/2008/03/currying-in-c-with-oliver-sturm.html



http://stackoverflow.com/questions/411572/proper-currying-in-c

1 comment:

Anonymous said...

I tend to agree, currying isn't really that useful in C# because you don't really save yourself much.

In Haskell, functions are curried by default, so 'multiply' looks like this (type signature optional):

multiply :: Int -> Int -> Int
multiply a b = a * b

multiply3 = multiply 3
multiplyResult = multiply3 15

putStrLn ("Result of 3 * 15 = " ++ (show multiplyResult))

This is really cool for all sorts of stuff, like multiplying all elements of a list by 5:

xs = [1,2,3]
ys = map (multiply 5) xs

This makes 'ys' equal to [5,10,15]. Map is the same as Select in Linq.