tags:

views:

2096

answers:

7

What reason is there for C# or java having lambdas? Neither language is based around them, it appears to be another coding method to do the same thing that C# already did.
I'm not being confrontational, if there is a reason I would like to know the reason why. For the purpose of full disclosure I am a Java programmer with a C++ background with no lisp experience, I may just be missing the point.

+6  A: 

I see lambdas in C# as a very convenient short-cut for doing delegates. Much more readable to have the code right there where it is being used rather than having to search elsewhere for the delegate definition.

tvanfosson
Anonymous functions and lambdas are different in C# - http://blogs.msdn.com/ericlippert/archive/2007/01/10/lambda-expressions-vs-anonymous-methods-part-one.aspx
johnstok
@johnstok - A little late to be pointing this out, but that article says that they're only different from the perspective of the implementer of a C# compiler (which the author of that blog is).
Daniel Earwicker
+1  A: 

Lambdas allow you write less verbose, more expressive code. For example, list comprehensions...

BTW, work is under way to explore the possibility of adding closures to Java - in the meantime it is necessary to use anonymous classes instead (ugly).

johnstok
+21  A: 

There are common use-cases which require passing (or storing) a block of code to be executed later. The most common would be event listeners. Believe it or not, the following bit of code uses a lambda-ish construct in Java:

JButton button = new JButton("Push me!");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Pressed!");
    }
});

The anonymous inner-class is acting as a lambda, albeit an extremely verbose one. With a little bit of implicit conversion magic, we can write the following equivalent in Scala:

val button = new JButton("Push me!")
button.addActionListener { e =>
  println("Pressed!")
}

C# makes this sort of thing fairly easy with delegates and (even better) lambdas.

Daniel Spiewak
I can see the reason why by your example, I can also see how it could cause a lot of confusion trying to debug until one learned lambdas extremely well.
WolfmanDragon
Actually, in my experience, lambda-rich code is *easier* to debug than the equivalent code written without lambdas. They're really pretty intuitive once you get your mind around them, and used judiciously, they can cut down on bulk (which obviously helps any code-related task, including debugging).
Daniel Spiewak
The subtle difference between delegate lambdas (more comparable to anonymous methods) and Expression lambdas is also a major consideration, but good post.
Marc Gravell
+3  A: 

Syntactic Sugar.

It provides a convenient and more-readable way to represent an idea, in this case a tiny throw-away method. Under the hood, the compiler expands that out to a delegate and method call, but it's the thing doing the work, not you.

Bob King
A: 

C# isn't going for purity to a particular school of language design (unlike Java, which was designed by the Smalltalkers as to be something of a pure OO language). C# is going for all-things-to-all-people, and it's pretty good at it. C# is based around gathering the best of the various styles of programming into one high-quality, well-supported language. That includes procedural, object-oriented, functional, dynamic, logic, etc. styles of programming. Obviously, so far it doesn't have much in the way of dynamic or logic styles of programming, but that is soon to come (dynamic programming coming with C# 4.0).

Justice
A: 

In case of C#, lambdas are used internally to implement LINQ. See the article The Evolution Of LINQ And Its Impact On The Design Of C#

Nemanja Trifunovic
A: 

Lambda's allow for more readable code in that they allow operations to be defined closer to the point of use rather than like the current C++ method of using function objects whose definition is sometimes far from the point of use. (This is not including some of the boost libraries). I think the key point from lambdas is that they allow more concise and easy to understand code.

Raindog