Ranges, constantly frustrating

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Wed Feb 12 13:01:58 PST 2014


On Wednesday, 12 February 2014 at 10:52:13 UTC, Regan Heath wrote:
> On Tue, 11 Feb 2014 19:48:40 -0000, Jesse Phillips 
> <Jesse.K.Phillips+D at gmail.com> wrote:
>
>> On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath 
>> wrote:
>>> Things like this should "just work"..
>>>
>>> File input ...
>>>
>>> auto range = input.byLine();
>>> while(!range.empty)
>>> {
>>>  range.popFront();
>>>  foreach (i, line; range.take(4))  //Error: cannot infer 
>>> argument types
>>>  {

> It isn't *required* to (input/forward), but it could (random 
> access).  I think we even have a template to test if it's 
> indexable as we can optimise some algorithms based on this.
>
>> What do you expect 'i' to be? Is it the line number? Is it the 
>> index within the line where 'take' begins? Where 'take' stops?
>
> If I say take(5) I expect 0,1,2,3,4.  The index into the take 
> range itself.

I don't see how these two replies can coexist. 'range.take(5)' is 
a different range from 'range.' 'range may not traverse in index 
order (personally haven't seen such a range). But more 
importantly you're not dealing with random access ranges. The 
index you're receiving from take(5) can't be used on the range.

Don't get me wrong, counting the elements as you iterate over 
them is useful, but it isn't the index into the range you're 
likely after. Maybe the number is needed to correspond to a line 
number.

>> There is a feature of foreach and tuple() which results in the 
>> tuple getting expanded automatically.
>
> And also the opApply overload taking a delegate with both 
> parameters.

I'm trying to stick with ranges and not iteration in general.



More information about the Digitalmars-d-learn mailing list