When does take modify the underlying iterator?
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 16 14:13:38 PDT 2016
On 8/16/16 5:01 PM, cy wrote:
> writeln("We take 1 from it...");
> writeln(iter.take(1));
> writeln("The rest of the range has:");
> writeln(iter.drop(1).array);
The fact that copying a forward range is generally the same operation as
.save is a huge problem. But there isn't really a way to deal with that.
In any case, I would probably write the code in the following way:
static if(isForwardRange!(typeof(iter)))
{
writeln(iter.save.take(1));
writeln(iter.drop(1)); // not sure why .array was here
}
else
{
writeln(iter.take(1));
writeln(iter);
}
But this may not work for any input range, since any time you copy the
range, you are copying internal state that may cache an element or more.
What we need is a take function that accepts input ranges by reference,
so it modifies the original. Then things get very strange and clunky.
-Steve
More information about the Digitalmars-d-learn
mailing list