Function literals and lambda functions

Jonathan M Davis jmdavisProg at gmx.com
Sun Mar 6 16:13:24 PST 2011


On Sunday 06 March 2011 06:03:31 Russel Winder wrote:
> OK, this one surprised me, all that remains is for me to find out why it
> shouldn't have done:
> 
>         reduce ! ( ( a , b ) { return a + b ; } ) ( 0.0 , outputData )
> 
> works just fine, but:
> 
>         reduce ! ( function double ( double a , double b ) { return a + b ;
> } ) ( 0.0 , outputData )
> 
> results in:
> 
>         pi_d2_sequentialMap.d(45): Error: function
> std.algorithm.reduce!(function double(double a, double b) {
>         return a + b;
>         }
>        
> ).reduce!(double,Map!(partialSum,Tuple!(int,int,double)[])).reduce.__funcl
> iteral1 cannot access frame of function
> pi_d2_sequentialMap.execute.__funcliteral1 pi_d2_sequentialMap.d(45):
> Error: function std.algorithm.reduce!(function double(double a, double b)
> {
>         return a + b;
>         }
>        
> ).reduce!(double,Map!(partialSum,Tuple!(int,int,double)[])).reduce.__funcl
> iteral1 cannot access frame of function
> pi_d2_sequentialMap.execute.__funcliteral1
> 
> which I think qualifies for the label "incomprehensible".  Not to
> mention repetitious.
> 
> 
> PS  If you ask why not:
> 
>         reduce ! ( "a+b" ) ( 0.0 , outputData )
> 
> I find this somehow unacceptable.  It's the string, its not a function.
> Fine, my problem, but that still leaves the above.

LOL. Whereas I find reduce!"a+b" _far_ clearer then having to write out the whole 
lambda function. Sure, there are cases where the string syntax doesn't cut it, 
but it's so much shorter and yet still perfectly clear (no extraneous parameter 
lists or braces or semicolons...), that I think that it's pretty much always 
preferred to actual, in-place lambda functions.

- Jonathan M Davis


More information about the Digitalmars-d mailing list