Anonymous function syntax

Jason House jason.james.house at gmail.com
Thu Sep 22 15:59:31 PDT 2011


std.algorithm already introduces a sort of lambda syntax...
map!"a+b"(...) or map!q{a+b}(...)

If D is looking for its own style of short lambda, maybe implicit use of a and b could be extended.
Candidates:
a+b
{a+b}
f{a+b}
auto{a+b}
auto a+b

Personally, I like the 2nd or 3rd one. The second is visually cleaner while the 3rd is easily parsed and feels very similar to q strings. I picked f because that's frequently used to represent a function. x could work, but that's a common variable name...

Walter Bright Wrote:

> I've collected a few from various languages for comparison:
> 
> D
>      (a,b) { return a + b; }
> 
> Ruby
>      ->(a,b) { a + b }
> 
> C++0x
>      [](int a, int b) { return a + b; }
> 
> C#
>      (a,b) => a + b
> 
> Scala
>      (a:Int, b:Int) => a + b
> 
> Erlang
>      fun(a, b) -> a + b end.
> 
> Haskell
>      \a b -> a + b
> 
> Javascript
>      function(a,b) { return a + b; }
> 
> Clojure
>      # (+ % %2)
> 
> Lua
>      function(a,b) return a + b end
> 
> Python
>      lambda a,b: a + b
> 



More information about the Digitalmars-d mailing list