source code bean

25 May, 2009

Functional programming in C# 3.0 and LINQ

Posted by: Peter In: C#|Functional programming

I first learned about functional programming a few years ago when I took a class in Haskell programming at my university, Chalmers. While Haskell might not be for everyday use, many of the concepts are very compelling, such as higher order functions, pattern matching, lazy evaluation, list comprehensions, lambda functions and map/fold/filter. Over the years many mainstream programming languages have adopted a subset of those features. With the release of C# 3.0 Microsoft has brought some of this functionality to C#.


Lambda functions
A lambda function is simply an anonymous function that can contain expressions and statements. Lambda functions can be used to create delegates or expression trees. Lambda functions use the lambda operator => (read as “goes to”). On the left side the input parameters is defined and the right side holds the expressions or statements. A simple example would be:

  1. (x, y) => x+y

But wait, where is the type declaration? The types does not have to be defined, the compiler will declare the types by using type interference at compile time, so everything is still strongly typed.

The framework defines a number of parameterized delegate types (the first type is the return type):

  1.  
  2. public delegate TR Func<TR>();
  3. public delegate TR Func<T0, TR>(T0 a0);
  4. public delegate TR Func<T0, T1, TR>(T0 a0, T1 a1);
  5. public delegate TR Func<T0, T1, T2, TR>(T0 a0, T1 a1, T2 a2);
  6. public delegate TR Func<T0, T1, T2, T3, TR>(T0 a0, T1 a1, T2 a2, T3 a3);
  7.  

So we could define our function as follows:

  1. Func<int,int,int> fun = (x, y) => x + y;

Map/fold/filter
In functional programming it is very common to work with lists. Three very common computations are:

    1. Applying a function to every element of a list (map)
    2. Computing a value based on all elements in the list. For example the total sum of a list containing numeric values (fold)
    3. Selecting just a subset of the elements (filter)

In C# the equivalents are Select, Aggregate and Where. Here is a simple example of how they can be used:

  1.  
  2. var list = new int[] { 1, 2, 3, 4, 5 };
  3. list.Aggregate(0, (total, val) => total += val); // returns 15, 0 is the initial value.
  4. list.Select(x => x + 1); // returns [2, 3, 4, 5, 6]
  5. list.Where(x => x > 3); // returns [4, 5]
  6.  

Using map/fold/filter instead of using regular loops makes it easier to understand and reason your code.



No Responses to "Functional programming in C# 3.0 and LINQ"

Comment Form


  • Joe: It looks like IceCoffee Script may provide the 'await' keyword to JavaScript. http://maxtaco.github.com/coffee-script/
  • Florian: Dealing with events has two primary fashions: imperative (synchronous) code, and callback (asynchronous) code. Both are legitimate styles, for some
  • Joe: I think we haven't seen true PaaS offering yet in Azure. If you use a webrole you're still running on your own VM. This is still not true multi-tena

About

Welcome to source code bean! On this site I post stuff that I encounter in my job and spare time. The content is mostly related to .NET development, but my interest in techonology is very broad, so often you will find posts on totally different subjects!