byChunk odd behavior?

Hanh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 26 10:19:44 PDT 2016


On Saturday, 26 March 2016 at 08:34:04 UTC, cym13 wrote:

> Sorry, it seems I completely misunderstood you goal. I thought 
> that take() consumed its input (which mostly only shows that I 
> really am careful about not reusing ranges). Writting a take 
> that consume shouldn't be difficult though:
>
>     import std.range, std.traits;
>     Take!R takeConsume(R)(auto ref R input, size_t n)
>         if (isInputRange!(Unqual!R)
>         && !isInfinite!(Unqual!R)
>     {
>         auto buffer = input.take(n);
>         input = input.drop(buffer.walkLength);
>         return buffer;
>     }
>
> but I think going with std.bitmanip/read may be the easiest in 
> the end.

Turns out bitmanip is actually using a loop.

foreach(ref e; bytes)
{
   e = range.front;
   range.popFront();
}

By the way, in your code above you are actually reusing the 
range: take is followed by drop and it won't work on an input 
range like 'byChunk'. That's the problem I ran into (see first 
post).



More information about the Digitalmars-d-learn mailing list