Ranges, constantly frustrating

Regan Heath regan at netmail.co.nz
Fri Feb 14 09:02:10 PST 2014


On Fri, 14 Feb 2014 12:29:49 -0000, Jakob Ovrum <jakobovrum at gmail.com>  
wrote:

> On Friday, 14 February 2014 at 12:10:51 UTC, Regan Heath wrote:
>> FWIW I disagree.  I think it's immediately and intuitively obvious what  
>> 'i' should be when you're foreaching over X items taken from another  
>> range, even if you do not know take returns another range.  Compare it  
>> to calling a function on a range and foreaching on the result, you  
>> would intuitively and immediately expect 'i' to relate to the result,  
>> not the input.
>>
>> R
>
> How should it behave on ranges without length, such as infinite ranges?

In exactly the same way.  It just counts up until you break out of the  
foreach, or the 'i' value wraps around.  In fact the behaviour I want is  
so trivial I think it could be provided by foreach itself, for iterations  
of anything.  In which case whether 'i' was conceptually an "index" or  
simply a "count" would depend on whether the range passed to foreach  
(after all skip, take, etc) was itself indexable.

> Also, `enumerate` has the advantage of the `start` parameter, which  
> usefulness is demonstrated in `enumerate`'s example as well as in an  
> additional example in the bug report.

Sure, if you need more functionality reach for enumerate.  We can have  
both;  sensible default behaviour AND enumerate for more complicated  
cases.  In my case, enumerate w/ start wouldn't have helped (my file was  
blocks of 6 lines, where I wanted to skip lines 1, 3, and 6 *of each  
block*)

> I'm not yet sure whether I think it should be implemented at the  
> language or library level, but I think the library approach has some  
> advantages.

Certainly, for the more complex usage.  But I reckon we want both  
enumerate and a simple language solution which would do what I've been  
trying to describe.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list