Function literals and lambda functions

Jacob Carlborg doob at me.com
Mon Mar 7 05:16:32 PST 2011


On 2011-03-07 01:13, Jonathan M Davis wrote:
> 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

Yes, but a different, less verbose, lambda syntax would be far better.

reduce((a, b) => a + b, 0.0, outputData)
-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list