Ranges, constantly frustrating
Regan Heath
regan at netmail.co.nz
Wed Feb 12 02:52:13 PST 2014
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
>> {
>> ..etc..
>> }
>> range.popFront();
>> }
>>
>> Tried adding 'int' and 'char[]' or 'auto' .. no dice.
>>
>> Can someone explain why this fails, and if this is a permanent or
>> temporary limitation of D/MD.
>>
>> R
>
> In case the other replies weren't clear enough. A range does not have an
> index.
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.
The reason I wanted it was I was parsing blocks of data over 6 lines - I
wanted to ignore the first and last and process the middle 4. In fact I
wanted to skip the 2nd of those 4 as well, but there was not single
function (I could find) which would do all that so I coded the while above.
> 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.
> byLine has its own issues with reuse of the buffer, it isn't inherent to
> ranges. I haven't really used it (needed it from std.process), when I
> wanted to read a large file I went with wrapping std.mmap:
>
> https://github.com/JesseKPhillips/libosm/blob/master/source/util/filerange.d
Cool, thanks.
R
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
More information about the Digitalmars-d-learn
mailing list