With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Partial application was covered to some extent in Chapter 8, but the use cases that were described there were rather simple. In the overall context of function construction, partial application deserves a bit more attention.

To remind you, currying is the conversion step from a multi-parameter function to a chain of single-parameter functions. It enables partial application, the technique of passing less than the complete set of parameters to a function in curried format, in order to create new functions out of existing ones.

Chapter 12 showed the standard higher order functions, and applying partial application to these functions creates many interesting opportunities. These functions tend to receive an algorithm as one of their parameters, in the form of an anonymous function, which they continue to apply to a list of elements. Partial application makes it easy to create very useful helper functions on the basis of these higher order functions, by applying the algorithm first and leaving the data to work against for later. Here’s a simple example of a function to square all the integer values in a sequence:

var curriedMap =

Functional.Curry<Converter<int, int>,

IEnumerable<int>, IEnumerable<int>>(Functional.Map<int, int>);

var squareList = curriedMap(x => x * x);

var list = new int[] { 2, 3, 4 };

var squaredList = squareList(list);

foreach (var item in squaredList)

Console.Write("{0} ", item);

Console.WriteLine( );

The curried Map function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required