<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Posts Tagged With 'Lambda Expressions' RSS Feed</title>
    <link>http://www.programmersheaven.com/blog/tags/Lambda+Expressions</link>
    <description>Contains the latest posts from the Programmer's Heaven blogs that are tagged with the label 'Lambda Expressions'</description>
    <lastBuildDate>Tue, 08 Jul 2008 21:17:19 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <item>
      <title>Higher Order Programming Is Easy!</title>
      <link>http://www.programmersheaven.com/user/pheaven/blog/133-Higher-Order-Programming-Is-Easy/</link>
      <description>Higher Order Programming is one of those things that to many people sounds weird, magical, mysterious or just too hard for them to be able to do. It's not. If you have ever passed a function or method as a parameter to another function or method, then you have done higher order programming. If you have ever used a function pointer in C or a delegate in C# or some kind of callback mechanism, then you have done higher order programming.&lt;br /&gt;
&lt;br /&gt;
=== Define it! ===&lt;br /&gt;
We are all used to writing code that takes parameters. We do it whenever we write subroutines, functions or methods. Normally these parameters are data. In higher order programming, one or more of these parameters is code rather than data. That is, you can pass code, functions and/or methods around, just as you can pass data around. That's all there is to it.&lt;br /&gt;
&lt;br /&gt;
=== Show me an example ===&lt;br /&gt;
Let's look at C#, because the syntax will not be too unusual to programmers in many languages. Suppose we have a List of scores for a test. &lt;br /&gt;
&lt;pre class="sourcecode"&gt;var Scores = new List&amp;lt;int&amp;gt;() { 2, 7, 9, 5, 6, 10, 8 };&lt;/pre&gt;&lt;br /&gt;
We want to get all scores that were considered passes. Suppose the test is out of ten, and to pass you need to get more than half marks. We can write a method to determine whether a score is a pass and that returns a boolean (true or false value).&lt;br /&gt;
&lt;pre class="sourcecode"&gt;public bool Pass(int Score)
{
    return Score &amp;gt; 5;
}&lt;/pre&gt;&lt;br /&gt;
Then we can use the FindAll method to find all scores that were considered passes.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;var Passed = Scores.FindAll(Pass);&lt;/pre&gt;&lt;br /&gt;
Notice what we have done here. We did not call the Pass method ourself. Instead, we passed the Pass method to the FindAll method as a parameter. Internally, it looped over the values in the list and called the Pass method on each of them, and put those that it returned true for into a new list and returned it.&lt;br /&gt;
&lt;br /&gt;
You are probably now thinking, why go to all the trouble when I could have just written a loop? And it's true - you could have written a loop that did the test in an if statement and build a new list.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;var Passed = new List&amp;lt;int&amp;gt;();
foreach (var Score in Scores)
    if (Score &amp;gt; 5)
        Passed.Add(Score);&lt;/pre&gt;&lt;br /&gt;
However, step back and think about what we just achieved from a software engineering perspective. It is a well known principle that separation of concerns helps to reduce code complexity. Here we have separated the concerns of:&lt;br /&gt;
* Testing each element of a list and building a new list of elements that passed the test&lt;br /&gt;
* The test itself&lt;br /&gt;
Therefore, higher order programming is a tool that enables us to reduce our code complexity by separating concerns, and also increase code re-use along the way.&lt;br /&gt;
&lt;br /&gt;
=== Code without a name ===&lt;br /&gt;
You might be thinking, "huh, I just wrote more code than I would have; I had to define a whole extra method!" You can also argue, quite reasonably, that we have actually made the program harder to understand now too, because you have to go and find the Pass method to fully understand the code.&lt;br /&gt;
&lt;br /&gt;
While sometimes we may have complex conditions that we wish to re-use in many places in our program - in which case writing a separate function or method is the right thing to do - in this case it would be nice if we could take advantage of higher order programming without having to resort to writing a separate method.&lt;br /&gt;
&lt;br /&gt;
Enter anonymous methods, or lambda expressions. These allow you to define a method (or function, depending on your language) without a name and inside another method. Let's re-write the previous example using a lambda expression.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;var Passed = Scores.FindAll(Score =&amp;gt; Score &amp;gt; 5);&lt;/pre&gt;&lt;br /&gt;
The "=&amp;gt;" is the syntax for defining an anonymous method in C# 3.0. Anything to the left of it is a parameter that the method takes. In this case, it takes a single parameter named "Score". To the right of it is the body of the method, with an implicit "return" (that is, it will return the boolean result of testing whether its parameter Score is greater than 5).&lt;br /&gt;
&lt;br /&gt;
You could read the "=&amp;gt;" quite nicely in this case as "where". That is, "find all scores where the score is greater than five".&lt;br /&gt;
&lt;br /&gt;
=== Yes, you can return code too ===&lt;br /&gt;
Suppose that I want to work out how many scores got different grades. For example, Grade A was given for scores between 8 and 10, Grade B for given for between 5 and 7, and so forth. We could write:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;var GradeA = Scores.FindAll(Score =&amp;gt; Score &amp;gt;= 8 &amp;amp;&amp;amp; Score &amp;lt;= 10);
var GradeB = Scores.FindAll(Score =&amp;gt; Score &amp;gt;= 5 &amp;amp;&amp;amp; Score &amp;lt;= 7);&lt;/pre&gt;&lt;br /&gt;
However, it would be nice if we could make something that read more clearly, such as:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;var GradeA = Scores.FindAll(Range(8,10));
var GradeB = Scores.FindAll(Range(5,7));&lt;/pre&gt;&lt;br /&gt;
How could this work? Well, FindAll expects a method that takes an int and returns a bool. This type of method - that is, a method with that signature - has a name: Predicate. Therefore, the Range method needs to have a return type of Predicate (actually, Predicate&amp;lt;int&amp;gt;, which means that the parameter it takes is of type int).&lt;br /&gt;
&lt;pre class="sourcecode"&gt;public Predicate&amp;lt;int&amp;gt; Range(int LowerLimit, int UpperLimit)
{
}&lt;/pre&gt;&lt;br /&gt;
And what do we write inside the Range method? Well, we return an anonymous function that takes a single parameter and tests if it is squeezed between the two values we supplied as parameters to the Range method.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;public Predicate&amp;lt;int&amp;gt; Range(int LowerLimit, int UpperLimit)
{
    return x =&amp;gt; x &amp;gt;= LowerLimit &amp;amp;&amp;amp; x &amp;lt;= UpperLimit;
}&lt;/pre&gt;&lt;br /&gt;
And that's it.&lt;br /&gt;
&lt;br /&gt;
=== What languages support this? ===&lt;br /&gt;
Here's the stinger: not all languages support these kinds of programming techniques. C# started out with the ability to do higher order programming, but without anonymous methods. In C# 2.0 they introduced anonymous methods, but with a fairly cumbersome syntax. In C# 3.0, they introduced the nice "lambda" syntax that I have shown here. Essentially, the language designers have realized more and more the value of higher order programming and worked to make it as easy as they make object oriented programming. The C# of today is a truly multi-paradigm language.&lt;br /&gt;
&lt;br /&gt;
Many of the dynamic languages provide good support for this, including Perl, Python and Ruby. C enables it through function pointers, though there's no anonymous method or lambda style syntax. Java, however, has so far chosen to stick to its object oriented roots rather than embracing the multi-paradigm approach. We'll leave arguing about whether that's a good or a bad thing for another time. And primarily functional languages, such as ML and Haskell, make extremely heavy use of higher order programming techniques.&lt;br /&gt;
&lt;br /&gt;
=== Taking it further ===&lt;br /&gt;
The examples I've shown here are trivial, but they are enough to give you a taste of what higher order programming is about. Try writing some code like this yourself, or reading up on higher order programming in a language of your choice. And when you're trying to work out a good way to get better code reuse and reduce code complexity, keep it in mind as one of the tools in your toolbox, alongside the other paradigms (such as object oriented programming, procedural programming and generic programming) that you are probably already more familiar with.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/user/pheaven/blog/133-Higher-Order-Programming-Is-Easy/</guid>
      <pubDate>Tue, 22 Jan 2008 07:09:07 -0700</pubDate>
      <dc:creator>pheaven</dc:creator>
    </item>
    <item>
      <title>C# 3.0 Part 2: Extension Methods &amp; Lambdas</title>
      <link>http://www.programmersheaven.com/user/pheaven/blog/84-C-30-Part-2-Extension-Methods--Lambdas/</link>
      <description>The second part of our C# 3.0 series is here, covering extension methods and lambda expressions. Learn:&lt;br /&gt;
* What an extension method is&lt;br /&gt;
* How to write extension methods&lt;br /&gt;
* The new lambda expression syntax&lt;br /&gt;
* To use the higher order programming paradigm&lt;br /&gt;
&lt;a href="http://www.programmersheaven.com/2/CSharp3-2"&gt;Go to the article!&lt;/a&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/user/pheaven/blog/84-C-30-Part-2-Extension-Methods--Lambdas/</guid>
      <pubDate>Tue, 27 Nov 2007 09:02:06 -0700</pubDate>
      <dc:creator>pheaven</dc:creator>
    </item>
  </channel>
</rss>