String lambdas

Seb via Digitalmars-d digitalmars-d at puremagic.com
Sun May 1 08:03:41 PDT 2016


On Saturday, 30 April 2016 at 15:10:08 UTC, Mint wrote:
> On Tuesday, 26 April 2016 at 19:45:18 UTC, Jack Stouffer wrote:
>> I'm of the opinion that string lambdas must go. I started, and 
>> I really should finish it at some point, removing string 
>> lambdas from the documentation: 
>> https://github.com/dlang/phobos/pull/3800
>>
>> I think that the drawback you mentioned does not outweigh the 
>> benefits gained from using actual lambdas.
>
> I'm personally somewhat fond of string lambdas for their 
> usefulness in making some operations very concise, without 
> sacrificing any readability.
>
> ie.
>
>   foo.map!"a.bar".reduce!"a * b";
>
> vs.
>
>   foo.map!(a => a.bar).reduce!((a, b) => a * b);
>
> I'm open to an alternative that is equally short and sweet, but 
> replacing them with proper lambda declarations feels like 
> uncalled for verbosity to me.

I really like them too and was impressed that Andrei is so 
against them, especially if you have more arguments writing the 
lambda get's more tedious.
The PR was created to support `each` string lambdas with more 
than two functions, e.g.:

arr.each!`a + 2*b + 4*c + 8*d`
arr.each!((a, b, c, d) = > a + 2*b + 4*c + 8*d)

While I agree that it might be a bit hard to read, they're 
beautifully concise!
Also they are quite popular - even in Phobos code.

> grep -r unaryFun * | wc -l

142

> grep -r binaryFun * | wc -l

250

> grep -r '!".*"' * --exclude datetime.d | wc -l

591 (disclaimer: some false positives)


Btw at mir we have the convention to use backticks, so it's 
easier to distinguish them from normal strings - might be a 
convention that you could adapt or even enforce?


More information about the Digitalmars-d mailing list