Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm nearly clueless as to what a lambda expression is, but I have a hard time believing that I couldn't finagle something to the same effect in Delphi (albeit with 900% more code).

What is the big advantage of lambda expressions, what am I missing out on and is there anything with the post Delphi 2009 RTL improvements that comes close to emulating the powers of C# in this regard?

share|improve this question
    
What's wrong with learning another language so you can learn about lambdas? Why are you sticking to Delphi to the exclusion of other languages? –  S.Lott Jan 4 '11 at 22:51
1  
@S.Lott I've got no qualms with learning other languages, but they'd probably be ones that are native to Unix and not used for exactly the same purpose as Delphi. I'm only a Delphi programmer on Windows because I work at a place where they're the only language and platform. –  Peter Turner Jan 4 '11 at 22:58
1  
"what am I missing out on" is trivially answered by learning another language. You say "no qualms" and then provide a bunch of things that sound like excuses. Take the hint. Pick a language and learn it. –  S.Lott Jan 5 '11 at 0:43
1  
@S.Lott, well apparently I'm not missing out on anything by retaining my relative ignorance! I didn't realize that anonymous methods and lambda expressions are two means to the same ends. I have dabbled in C# and I know C++ well enough and I don't believe there is a big enough difference between C# and Delphi to even consider learning C# as an accomplishment. –  Peter Turner Jan 5 '11 at 14:11
1  
Here's my point (again). Please continue to "dabble" in other languages. Then, accelerate your dabbling to learn another language. It's good to learn more languages. Keep doing it. –  S.Lott Jan 5 '11 at 16:13
add comment

4 Answers 4

up vote 5 down vote accepted

Let's say in Delphi you have anonymous inline methods like the delegate in this C# code:

var squared = Enumerable.Range(0, 100)
    .Select(
         delegate(int a)
         {
             return a * a;
         }
    );

Then the lambdas are simply a shorter way of writing that:

var squared = Enumerable.Range(0, 100)
    .Select(
         a => a * a
    );

The code snippets above are identical from the IL point of view.

share|improve this answer
    
BTW I'm also using LINQ in this example. The .Select() statement is an extension method introduced by this technology. –  Jader Dias Jan 4 '11 at 19:02
add comment

You're not missing anything, really. Lambdas are just a different form of anonymous methods, which were introduced in Delphi 2009, with a very minimal syntax that makes them cryptic and hard to read. (No function header to speak of, for example, which means that you can't tell the type of the variables you're working with by looking at the code.)

share|improve this answer
1  
Does this answer refer to Delphi's version of lambdas, or to lambdas in general? –  Larry Coleman Jan 4 '11 at 18:52
2  
They are not cryptic nor hard to read. No, no wait a minute, that's exactly what I thought the first time I saw one! ¬¬ You just have to get use to them. On the other hand, I had the same problem with array literals, ternary operator, switch statement and pretty much anything the first time. Once you get used to them, you find the exact opposite effect, they are clean and expressive. –  OscarRyz Jan 4 '11 at 19:24
2  
@Mason: If the expression in the lambda is too complex for you to tell what the type is just by looking, it shouldn't be in a lambda. –  Larry Coleman Jan 4 '11 at 19:36
2  
@Larry: Yeah, so you have to go and rewrite it as a normal method. I guess my objection is that I just tend to view "terse" code in general as a form of premature (and improper) optimization. It's optimizing for writing time at the expense of maintainability, which has been shown over and over again to comprise a far more significant piece of developing a program than the initial writing. –  Mason Wheeler Jan 4 '11 at 20:01
4  
@Mason Wheeler: verbose code does not necessarily equal maintainable code. You (Mason Wheeler) wouldn't expect to reiterate the type of a variable every time you (Mason Wheeler) used it in a normal expression any more than you (Mason Wheeler) would expect to continually address someone by their full name in conversation - indeed, you (Mason Wheeler) might quickly find such verbosity a distraction! Use lambdas in situations where the information necessary to understand the expression is readily available, and as with any other expression factor it out once that condition no longer applies. –  Shog9 Jan 5 '11 at 0:54
show 10 more comments

I have a hard time believing that I couldn't finagle something to the same effect in Delphi (albeit with 900% more code).

Well... That's what you're missing out on then - being able to accomplish certain things without writing a huge amount of plumbing.

It's syntactic sugar, in the same sense that things like bounded loops, functions, and user-defined types are sugar - it provides a means of both simplifying and writing code that clearly expresses what you're doing without getting bogged down in the petty details of how you're wrestling the CPU into actually doing it.

share|improve this answer
    
Well is it the difference between writing my code in assembler and writing it in C or the difference between writing an SQL query and using LINQ (another technology I'm ignorant of) –  Peter Turner Jan 4 '11 at 18:38
    
@Peter: LINQ and SQL are fairly similar (in what they represent, not in terms of syntax). This is probably closer to doing loops in asm, or objects in C. –  Shog9 Jan 4 '11 at 18:49
add comment

The "900% more code" you mention in your question makes an even bigger difference than you think. It means in practice that you just do without the feature in question, because who's going to write 900% more code? I'll give you an example. A couple of years ago, I wrote a SQL parser in Haskell using a parser combinator library called Parsec. A colleague asked why I didn't use C# to write it. My answer was that if I had to write it in C#, it would have been twenty times as much code or more, so I wouldn't have written it at all.

share|improve this answer
    
The 900% more code he mentions is also a severe exaggeration. While I agree that anonymous methods in Delphi could use some syntactic reduction, they're really not all that bad, especially for the amount of information they provide to the reader. –  Mason Wheeler Jan 4 '11 at 18:59
1  
I guess you're right - but our programming paradigms are different. If a Delphi programmer doesn't want to write a lot of code he usually does something lazy or stupid instead. To me, it seems like if a programmer using a language that stresses functional programming doesn't want to write a lot of code he conjures up some wizardry. –  Peter Turner Jan 4 '11 at 19:00
2  
@Mason, I'd say it's twice as much code with anonymous methods, 900% in any language to get the same effect without anonymous methods. en.wikipedia.org/wiki/Anonymous_function#Delphi <- is a bad example of the differences in lines of code, but Delphi certainly is the most understandable of the bunch. –  Peter Turner Jan 4 '11 at 19:03
    
@Mason, I don't know Delphi, but I've seen code reductions of that order of magnitude in other contexts. –  Larry Coleman Jan 4 '11 at 19:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.