A Small Contribution to Phobos

bearophile bearophileHUGS at lycos.com
Sun Jun 2 06:20:29 PDT 2013


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

And using map with a lambda that returns nothing is not a style I 
like :-( It's probably better to encourage D programmers to give 
pure lambdas to map/filter, for several reasons (safety, 
cleanness, code style, future D front-end optimizations done on 
those higher order functions, to allow a better debuggability, 
and to avoid Phobos bugs like 
http://d.puremagic.com/issues/show_bug.cgi?id=9674 ). So I think 
it's better to introduce a new Phobos function like tap() that 
accepts a function/delegate with side effects that takes no input 
arguments.

Bye,
bearophile


More information about the Digitalmars-d mailing list