std.algorithm missing for_each?

bearophile bearophileHUGS at lycos.com
Wed Feb 5 03:17:47 PST 2014


Jens Mueller:

> Another option is to iterate using the foreach loop. Does not 
> look better either, does it?
>
> ...
>
> myRange
> .each!((e)
> {
> 	e.someOperation();
> });

Adding something like each() has being proposed, but so far no 
one has implemented it for Phobos.

But the need of each() is not strong, there are more important 
functions to add, like a sum() (being implemented by Andrei), and 
more.

Using foreach() is not bad, it shows clearly to the reader that 
the code is doing something imperative. So it breaks the style of 
the UFCS chains, but such breakage reflects the semantic breakage.

And the syntax of the each() you have written is a little less 
readable than a foreach (unless the usage of jQuery and the like 
has trained your eyes to not see them). One good thing of each() 
is that it at the end of the chain, while you need to put foreach 
at its start. This is not handy and it's enough to ask for a 
each() in Phobos.

To improve the syntax of each() and make similar imperative 
procedures look more like language constructs, some persons have 
suggested an alternative syntax sugar for functions that have a 
delegate as last argument:

myRange
.each {
     e.someOperation;
};

Bye,
bearophile


More information about the Digitalmars-d mailing list