24

I don't really get lambda expressions. While they've been around since the days of ALGOL, I didn't start hearing about them until fairly recently, when Python and Ruby became very popular. Now that C# has the => syntax, people in my world (.NET) are talking about lamdba expressions more and more.

I've read the Wikipedia article on the lambda calculus, but I'm not really a math guy. I don't really understand it from a practical perspective. When would I use lambda expressions? Why? How would I know that it's what I should be doing?

Can you show examples of how you would solve problems with lambda expressions, in a before-and-after format? Any imperative language is fine, but C# would be easiest for me to understand.

Robert S.
  • 24,920
  • 14
  • 82
  • 115
  • 1
    duplicates http://stackoverflow.com/questions/167343/c-lambda-expression-why-should-i-use-this http://stackoverflow.com/questions/150129/what-is-a-lambda – jop Oct 09 '08 at 14:13
  • Thanks, I read those links but they didn't answer my question. My question is a little more specific as to examples of how this particular technology is useful and the determination of that use. – Robert S. Oct 09 '08 at 14:18

6 Answers6

22

Basically as far as C# is concerned, lambda expressions are an easy way to create a delegate (or an expression tree, but let's leave those aside for now).

In C# 1 we could only create delegate instances from normal methods. In C# 2 we gained anonymous methods. In C# 3 we gained lambda expressions, which are like more concise anonymous methods.

They're particularly concise when you want to express some logic which takes one value and returns a value. For instance, in the context of LINQ:

                       // Only include children - a predicate
var query = dataSource.Where(person => person.Age < 18) 
                       // Transform to sequence of names - a projection
                      .Select(person => person.Name);

There's a fuller discussion of this - along with other aspects - in my article on closures.

Robert S.
  • 24,920
  • 14
  • 82
  • 115
Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • I saw this question and I thought, "Ooo! Maybe I can beat Jon Skeet to the punch!" But then I didn't. :) +1 – Greg D Oct 09 '08 at 14:12
  • 1
    that code is looking pretty nice, even though I am pondering if Select is the right name. if you used Map, you'd arrive at relational algebra in code. :) – Tetha Oct 09 '08 at 14:15
  • To me, Map suggests the creation of a map from key to value, where you can look up by key. Personally I'd prefer Project(). – Jon Skeet Oct 09 '08 at 14:16
  • There was a brief, tangentially related discussion on Lippert's blog earlier this year about reading the "=>" operator: http://blogs.msdn.com/ericlippert/archive/2008/05/16/reading-code-over-the-telephone.aspx – Greg D Oct 09 '08 at 14:20
3

The answers to this question might be useful to you.

Community
  • 1
  • 1
Chris Young
  • 15,295
  • 6
  • 35
  • 42
2

lambda functions are just anonymous functions.

For example, in python, you want to double all elements in a list. There are pretty much three ways to do so:

List-expressions:

[2*x for x in list]

explicit function:

def double(x):
    return 2*x
map(double, list) # iirc

with lambdas:

double = lambda x : 2*x
map(double, list)

So, a lambda in most languages is just a way to avoid the syntactic overhead of creating a new function.

Tetha
  • 4,750
  • 1
  • 15
  • 17
1

This is one of the better explanations I've seen of how to understand the big ideas in using lambda expressions in C#:

http://www.developingfor.net/c-30/upgrade-your-c-skills-part-3-lambda-expressions.html

Romano Zumbé
  • 7,735
  • 4
  • 32
  • 52
0

My main use of lambda expressions in .NET has been when working with lists. Using a lambda expression you can build up a query on a list in a similar way as you would build an SQL statement to search a database table.

Jimmeh
  • 2,778
  • 1
  • 21
  • 21
0

A lambda expression is a functional value (in the same sense that 'int' and 'string' are values). This means that it is possible to

  • define a variable that holds a lambda expression (this is the same as defining a function)
  • return a lambda expression from a method or function
  • send a lambda expression as an argument to a function or method

Take a look at functions like map, filter or fold (in any functional language) to see how lambda expressions are used as arguments to other functions.

Jonas
  • 18,842
  • 10
  • 53
  • 67