Sat, 18 Dec 2010 19:09:24 +0100, Jacob Carlborg wrote: > As Nick writes here the Scala/C#-style syntax is one suggestion. There > are also several other syntaxes available, one just have to look at > other languages to get ideas. Here's a list of the syntax used by a > couple of different language, some languages are list more than once > because they support more than one syntax. I've listed the languages in > order from, what I think, the least verbose to the most verbose > lambda/delegate syntax (the number in front of the languages is the > level of verbose, if two languages are at the same level I think they > are equally verbose). > > 1 D: foo(writeln(3)); // lazy argument
That's not really equivalent to lambdas. It would be unfair to not mention Scala which also supports lazy arguments. > 1 Scala: foo(_ * _) This isn't the same. _ * _ is equivalent to (a, b) => a * b 1 Scala: foo(x => x * x) > 2 C#: foo(x => x * x); > 3 Scala: foo((x) => x * x) foo(x => x * x) also works in this case > 4 Python: foo(lambda x: x * x) > 5 Ruby: foo { |bar| x * x } Maybe you meant foo { |x| x * x } > 5 Ruby: foo do |x| x * x end > 6 D: foo((int x) { return x * x; }); > 7 C++1x: foo([](int x){ return x * x; }); > 7 Apple's (Objective)-C(++) > block extension: foo(^(int x){ return x * x; }); > 8 JavaScript: > foo(function(x){ return x * x }) > 9 PHP: foo(function ($x) use($fooBar) { > return $x * $x; }); // "use" is used for explicitly telling what > variables should be available when the scope is gone. > > Note that I have not listed any functional languages here because I've > never used one. For example: Lambda calculus: λx.x*x Haskell: \x -> x * x As you can see, most of the verbosity comes from the fact that lambdas in D and C++ contain statements, not a single expression. It's like if-then- else vs ternary ?: -- In languages like Scala these are the same built-in feature.