Ranges, constantly frustrating

Regan Heath regan at netmail.co.nz
Thu Feb 13 06:30:40 PST 2014


On Wed, 12 Feb 2014 21:01:58 -0000, Jesse Phillips  
<Jesse.K.Phillips+D at gmail.com> wrote:

> 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.

You chopped of your own comment prompting this response, in which I am  
responding to a minor side-point, which I think has confused the actual  
issue.  All I was saying above was that a range might well have an index,  
and we can test for that, but it's not relevant to the foreach issue below.

>>> 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.'

Yes, exactly, meaning that it can trivially "count" the items it returns,  
starting from 0, and give those to me as 'i'.  *That's all I want*

> '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.

A forward range can do what I am describing above, it's trivial.

> 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.

Nope, not what I am after.  If I was, I'd iterate over the original range  
instead or keep a line count manually.

> Maybe the number is needed to correspond to a line number.

Nope.  The file contains records of 5 lines plus a blank line.  I want 0,  
1, 2, 3, 4, 5 so I can skip lines 0, 2, and 5 *of each record*.

R

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


More information about the Digitalmars-d-learn mailing list