foreach

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 13:51:08 PDT 2014


On Fri, 13 Jun 2014 01:27:24 -0400, Manu via Digitalmars-d  
<digitalmars-d at puremagic.com> wrote:

> On 13 June 2014 14:14, Steven Schveighoffer via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>
>>> Some forward ranges don't have a known length, and can only be summed
>>> by an iteration sweep.
>>
>>
>> http://dlang.org/phobos/std_range.html#.walkLength
>
> That's inefficient, I might as well perform the iteration while I'm
> walking. No need for a whole pre-pass.

It's the same complexity, O(n). I'm not sure how inefficient it is.  
Depending on what popFront does, the cache impact may actually make it  
perform better to do popFront in a tight loop, then do the iterations in a  
loop later.

>
> Anyway, you don't need to show me how to work-around the issue. Of you
> think I'm not capable of figuring out workarounds myself, then I'm
> rather insulted.

Please, don't feel insulted, I was not suggesting you didn't know how to  
work around the issue. It seemed you weren't aware of what walkLength did.

> The point is, it's crappy, I see no real reason not to support
> foreach(; range) or foreach(range), it is already precedented by
> for(;;) (I was initially surprised by inconsistency, I'm sure I'm not
> alone). I was just throwing it out there as a thought.

I will note that your original request (I thought) was specifically for  
foreach(;x..y), which is not a D range. I can't think of any useful cases  
where one wants to iterate over a range and not use the data in the range.

The improvement to the language is minimal, but obviously not zero. I just  
don't think it's worth the benefit to change the syntax. That's all I'm  
saying.

-Steve


More information about the Digitalmars-d mailing list