std.algorithm missing for_each?

Idan Arye GenericNPC at gmail.com
Wed Feb 5 03:50:44 PST 2014


On Wednesday, 5 February 2014 at 11:21:12 UTC, bearophile wrote:
>> myRange
>> .each {
>>    e.someOperation;
>> };
>
> Possibly better:
>
> myRange
> .each (e) {
>     e.someOperation;
> };
>
>
> Bye,
> bearophile

I is unclear from the syntax whether `(e)` is a the argument list 
and `{...}` is a block lambda or the property syntax is used and 
`(e) {...}` is a regular lambda.

How about using the Ruby style of blocks:

     myRange
     .each do(e) {
         e.someOperation;
     };

This will also give a nice syntax when passing named functions:

     myRange.each do writeln;



Alternatively, we can fuse the delegate definition with the 
higher order function's call syntax like this:

     myRange
     .each (auto e) {
         e.someOperation;
     };

Here, `(auto e)` *is* the argument list of `each`, but it's clear 
that `e` is not an argument passed to `each` because it uses the 
declaration syntax rather than being an expression. Actual 
arguments could be passed in the same list:

     forEachInRange(auto i, 0, 10) {
         ...
     }

This, ofcourse, will require some special syntax for declaring 
the higher order function.


More information about the Digitalmars-d mailing list