Is there something like a consuming take?

a11e99z black80 at bk.ru
Sat Jul 6 11:48:51 UTC 2019


On Saturday, 6 July 2019 at 11:20:50 UTC, berni wrote:
> I want to copy the first n items of a range to an array, I came 
> up with this now:
>> data = r.take(n).array;
> This works partly, because the values of r are not consumed. So 
> I have to call afterwards:
>> r = r.drop(n);
> Now I wonder, if it is possible to do this with one single 
> call, something like
> data = r.take_consuming(n).array;
> Does there something like this exist?

sure
auto take_consuming( R )( ref R r, int cnt ) {
     auto tmp = r.take( cnt ).array;
     r = r.drop( cnt );
     return tmp;
}
don't thank


More information about the Digitalmars-d-learn mailing list