4

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?

6
  • 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. Commented Jan 4, 2011 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
    Commented Jan 5, 2011 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. Commented Jan 5, 2011 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
    Commented Jan 5, 2011 at 16:13
  • 1
    @S.Lott Delphi users don't learn other languages. That would be "disloyal" and would suggest that Delphi isn't the ultimate language, only held back because other languages are considered more cool or popular.
    – alcalde
    Commented Mar 22, 2017 at 20:46

4 Answers 4

5

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.

1
  • BTW I'm also using LINQ in this example. The .Select() statement is an extension method introduced by this technology.
    – Jader Dias
    Commented Jan 4, 2011 at 19:02
6

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.)

15
  • 1
    Does this answer refer to Delphi's version of lambdas, or to lambdas in general? Commented Jan 4, 2011 at 18:52
  • 3
    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
    Commented Jan 4, 2011 at 19:24
  • 3
    @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. Commented Jan 4, 2011 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. Commented Jan 4, 2011 at 20:01
  • 6
    @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
    Commented Jan 5, 2011 at 0:54
4

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.

2
  • 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) Commented Jan 4, 2011 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
    Commented Jan 4, 2011 at 18:49
2

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.

4
  • 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. Commented Jan 4, 2011 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. Commented Jan 4, 2011 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. Commented Jan 4, 2011 at 19:03
  • 1
    @Mason, I don't know Delphi, but I've seen code reductions of that order of magnitude in other contexts. Commented Jan 4, 2011 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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