Accessors, byLine, input ranges

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Jan 29 09:09:45 PST 2010


Michel Fortin wrote:
> On 2010-01-29 11:18:46 -0500, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> said:
> 
>> It should work for stream ranges if front() checks the "filled" flag 
>> and eats the next line if false, and popFront clears the "filled" flag.
> 
> So now you want the front to fetch from stdin on the first call? It's 
> the same problem as 'byLine' eating the first line when you create it: 
> neither one or the other should affect the stream.
> 
> Take my example from before:
> 
>     // fetch 3 lines:
>     string[] consume(3, stdin.byLine);
>     // now fill a buffer
>     ubyte[] buffer;
>     buffer = stdin.rawRead(buffer);
> 
>     E[] consume(E, R)(int count, R range) {
>         E[] elements;
>         foreach (i; 0..count) {
>             elements ~= range.front;
>             range.popFront();
>         }
>         return elements;
>     }
> 
> Now add a range.front just after the popFront():
> 
>     E[] consume(E, R)(int count, R range) {
>         E[] elements;
>         foreach (i; 0..count) {
>             elements ~= range.front;
>             range.popFront();
>             E peakedElement = range.front;
>         }
>         return elements;
>     }
> 
> And suddenly you'll get the same problem.

I think this is not a problem as much as an organic characteristic of 
streams. STL's input streams work the same.

Andrei



More information about the Digitalmars-d mailing list