Is there something like a consuming take?

Adam D. Ruppe destructionator at gmail.com
Sat Jul 6 14:48:04 UTC 2019


On Saturday, 6 July 2019 at 14:40:23 UTC, berni wrote:
>>        .byChunk(BUFFER_SIZE)

byChunk is defined to reuse its buffer between calls.

http://dpldocs.info/experimental-docs/std.stdio.byChunk.1.html#examples

This means previous contents are overwritten when you advance.


> When I now change BUFFER_SIZE to 2 I get:
>
>>[ 1, 2, 3, 4, 5 ]
>>[ 5, 6, 7, 8, 9, 10 ]
>
> Now the first two buffers have been consumend and the third 
> ([5, 6]) not.

So here, the take call gave you a view into the first 5 elements. 
It read one and two and printed them, then byChunk.popFront was 
called, overwriting the buffer with 3,4 and take passed that to 
writeln, then popFront again, overwriting with 5,6.

writeln printed out 5, and take, having finished its work, left 
the buffer alone in its state.


Now, you print the other thing, which still has 5,6 in the 
buffer, then popFront, overwrites with 7,8, etc and so on.


So this is a case of input range behavior - always consuming the 
underlying file - combined with buffering of two elements at 
once, leaving 5,6 behind, and the reuse of the buffer meaning you 
see that 5,6 again on the next call.


More information about the Digitalmars-d-learn mailing list