VLERange: a range in between BidirectionalRange and RandomAccessRange

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Jan 17 22:11:04 PST 2011


On 1/17/11 11:48 PM, Jonathan M Davis wrote:
> On Monday 17 January 2011 15:13:42 spir wrote:
>> See range bug evoked above. opApply is the only workaround AFAIK.
>> Also, ranges cannot yet provide indexed iteration like
>> 	foreach(i, char ; text) {...}
>
> While it would be nice at times to be able to have an index with foreach when
> using ranges, I would point out that it's trivial to just declare a variable
> which you increment each iteration, so it's easy to get an index even when using
> foreach with ranges. Certainly, I wouldn't consider the lack of index with
> foreach and ranges a good reason to use opApply instead of ranges. There may be
> other reasons which make it worthwhile, but it's so trivial to get an index that
> the loss of range abilities (particularly the ability to use such ranges with
> std.algorithm) dwarfs it in importance.
>
> - Jonathan M Davis

It's a bit more difficult than that. When iterating a variable-length 
encoded range, what you need more than the current item being iterated 
is the physical offset reached inside the range. That's not all that 
difficult either as the range can always provide an extra primitive, but 
a bit annoying (e.g. because it makes iteration with foreach impossible 
if you want the index, unless you return a tuple with each step).

At any rate, I agree with two things - one, we need to fix the foreach 
situation. Two, even before we find a fix, at this point committing to 
iteration with opApply essentially commits the iteratee to an island 
where all basic algorithms need to be reinvented from first principles.


Andrei


More information about the Digitalmars-d mailing list