Visual Studio 2010: Lambdas may be theoretically based in Calculus, but they are easy and useful
Many of us just spent a decade or so with languages designed from the ground up as object oriented languages using dynamic dispatch, with syntax similar to C++.
The notion of a Lambda Expression is new. Lambdas sometimes seem a little syntactically sugary, but ultimately I enjoy the grammatical brevity of them. I reminisce about function pointers but I will show you some unique differences below.
Warning: These examples compile under Visual Studio 2010 Beta 1.
Lambdas are well documented, but nothing helps more than some real code snippets.
Note the code below results in j holding 25.
|
The Lambda Operator => ( The new character here is “=>”)
It is the lambda operator =>, which is read as "goes to”. Note some basic syntax in using the lambda operator. The code below illustrates a similar string version.
|
This block of code uses lambdas to iterate through an array of integers.
oddNumbers The MessageBox shows: |
Numbers less than 6
firstNumbersLessThan6 gets the numbers {5, 4, 1, 3}, which makes perfect sense given the snippet below:
Here is the view from the debugger: |
Multiple Parameters in Lambdas
Lambdas show up in where clauses too
|
Only 3 rules for Lambdas – I’m telling you it’s up to the delegates parameters and return types
The general rules for lambdas are as follows:
|
More sophisticated scenarios
Let’s use Lambdas to do more sophisticated searches and filters. Assume the following code:
|
The next method to execute is “AnonMethod()”
Let’s do the same thing with the Lambda example.
Note that line 3 is the alternate syntax
|
Example: Using Two Parameters in a Lambda Expression
Example: Using a where clause on a list of integers
Source Code: The Entire Sample Class discussed above
If you look carefully here, you can see how you might want to create your own Lambda method. FilterBy<K,V>, where K is a dictionary of items, and V is KeyValueFilter.
static class MyLambda
{
public static void TestHarness()
{
List<string> list = new List<string>();
list.Add("AA");
list.Add("ABC");
list.Add("DEFG");
list.Add("XYZ");
Console.WriteLine("Through Anonymous method");
AnonMethod(list);
Console.WriteLine("Through Lambda expression");
LambdaExample(list);
Dictionary<string, int> varClothes = new Dictionary<string, int>();
varClothes.Add("Jeans", 20);
varClothes.Add("Shirts", 15);
varClothes.Add("Pajamas", 9);
varClothes.Add("Shoes", 9);
var ClothesListShortage = varClothes.FilterBy((string name, int count) => name == "Shoes" && count < 10);
// example of multiple parameters
if (ClothesListShortage.Count > 0)
Console.WriteLine("We are short of shoes");
}
static void AnonMethod(List<string> list)
{
List<string> evenNumbers = list.FindAll
(
delegate(string i)
{
return (i.Length % 2) == 0;
}
);
foreach (string evenNumber in evenNumbers)
{
Console.WriteLine(evenNumber);
}
}
static void LambdaExample(List<string> list)
{
var evenNumbers = list.FindAll(i => (i.Length % 2) == 0);
foreach (string i in evenNumbers)
{
Console.WriteLine(i);
}
}
public static Dictionary<K, V> FilterBy<K, V>(this Dictionary<K, V> items, KeyValueFilter<K, V> filter)
{
var result = new Dictionary<K, V>();
foreach (KeyValuePair<K, V> element in items)
{
if (filter(element.Key, element.Value))
result.Add(element.Key, element.Value);
}
return result;
}
}
Comments
- Anonymous
July 21, 2009
<a href="http://www.AirCharterGuru.com">Jeff Alan</a> <a