<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>source code bean &#187; Functional programming</title>
	<atom:link href="http://sourcecodebean.com/archives/category/functional-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://sourcecodebean.com</link>
	<description>giving you tricks and tips of good coding</description>
	<lastBuildDate>Sun, 06 Jun 2010 16:40:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Functional programming in C# 3.0 and LINQ</title>
		<link>http://sourcecodebean.com/archives/lambda-expressions-in-c-30/199</link>
		<comments>http://sourcecodebean.com/archives/lambda-expressions-in-c-30/199#comments</comments>
		<pubDate>Mon, 25 May 2009 13:26:17 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Functional programming]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=199</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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#. </p>
<p><strong>Lambda functions</strong><br />
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:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> =&gt; x+y</div>
</li>
</ol>
</div>
<p>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. </p>
<p>The framework defines a number of parameterized delegate types (the first type is the return type):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;TR&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;T0, TR&gt;<span class="br0">&#40;</span>T0 a0<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;T0, T1, TR&gt;<span class="br0">&#40;</span>T0 a0, T1 a1<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">public delegate TR Func&lt;T0, T1, T2, TR&gt;<span class="br0">&#40;</span>T0 a0, T1 a1, T2 a2<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">public delegate TR Func&lt;T0, T1, T2, T3, TR&gt;<span class="br0">&#40;</span>T0 a0, T1 a1, T2 a2, T3 a3<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>So we could define our function as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Func&lt;int,<span class="kw4">int</span>,int&gt; fun = <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> =&gt; x + y; </div>
</li>
</ol>
</div>
<p></p>
<p><strong>Map/fold/filter</strong><br />
In functional programming it is very common to work with lists. Three very common computations are:</p>
<ol>1. Applying a function to every element of a list (map)</ol>
<ol>2. Computing a value based on all elements in the list. For example the total sum of a list containing numeric values (fold)</ol>
<ol>3. Selecting just a subset of the elements (filter)</ol>
<p>In C# the equivalents are <code>Select</code>, <code>Aggregate </code>and <code>Where</code>. Here is a simple example of how they can be used:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">var list = new <span class="kw4">int</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#123;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">5</span> <span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">list.<span class="me1">Aggregate</span><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="br0">&#40;</span>total, val<span class="br0">&#41;</span> =&gt; total += val<span class="br0">&#41;</span>; <span class="co1">// returns 15, 0 is the initial value. </span></div>
</li>
<li class="li1">
<div class="de1">list.<span class="me1">Select</span><span class="br0">&#40;</span>x =&gt; x + <span class="nu0">1</span><span class="br0">&#41;</span>; <span class="co1">// returns [2, 3, 4, 5, 6]</span></div>
</li>
<li class="li2">
<div class="de2">list.<span class="me1">Where</span><span class="br0">&#40;</span>x =&gt; x &gt; <span class="nu0">3</span><span class="br0">&#41;</span>; <span class="co1">// returns [4, 5]</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Using map/fold/filter instead of using regular loops makes it easier to understand and reason your code.  </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/lambda-expressions-in-c-30/199/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
