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

Categories

Adwords

Twitter Updates


    • Petan: I got in bootstrap this (insted of $frontController->getRouter()): $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'rout
    • oanh tong ngoc: :) It's usefull but could U give everyone's an example with a project source code. Thanks
    • Peter: Hi Sohaib, It seems like the rewrite module isn't loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the ser

    About

    Welcome to source code bean! You will find information on tips and tricks on programming languages, server side stuff, and anything that causes troubles to web development.