A Small Contribution to Phobos

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jun 2 09:33:15 PDT 2013


On 6/2/13 9:20 AM, bearophile wrote:
> Andrei Alexandrescu:
>
>> [1, 2, 3, 4].map!(n => n.writeln).reduce;
>
> I have to shot this down for many reasons:
>
> I think it's better to give that final function a different name (like
> "consume" or something like that) because it's used for very different
> purposes and it returns nothing. Re-using the name "reduce" doesn't
> reduce the amount of Phobos lines of code, it doesn't make the user code
> simpler to understand, it's more obscure because it's more semantically
> overloaded, and it's not more easy to find in the documentation by the
> future D users.

Au contraire, there are many advantages. Using "reduce" leverages a 
well-understood notion instead of introducing a new one. There is less 
need for documentation, motivation, and explanations. "Reduce with no 
function simply spans the entire range." Builds on an already-eager 
construct par excellence instead of adding a new one that must be 
remembered and distinguished from the lazy constructs.

Actually my first thought when I saw consume() was to look up reduce, 
thinking, "how do I reduce a range to nothing"? Because that's the goal. 
Reduce is the obvious choice here.

> Function names are not language keywords, packing different purposes
> in the same name as "static" doesn't give any advantage, and only
> disadvantages.

Strawman argument.


Andrei


More information about the Digitalmars-d mailing list