Another cool mini-project: advance a range within n steps from its end

wobbles via Digitalmars-d digitalmars-d at puremagic.com
Fri Dec 4 13:26:01 PST 2015


On Friday, 4 December 2015 at 20:01:10 UTC, Jacob Carlborg wrote:
> On 2015-12-04 17:37, Andrei Alexandrescu wrote:
>> Like "tail" in Unix. Given a range R r and a number size_t n, 
>> return a
>> TakeExactly!R that's r at no more than n steps from its end:
>>
>> TakeExactly!R advanceWithin(R)(R r, size_t n)
>> if (isForwardRange!R);
>>
>> Invariant:
>>
>> assert(r.advanceWithin(n).length <= n);
>>
>> Implementation would send a scout range ahead, etc.
>>
>> I didn't file an issue for it, but it's a great function to 
>> have.
>
> retro + take?

+ retro to turn it back the normal way?

Also, I think this'd work?
return r.takeExactly(r.walkLength - n);

It wouldn't be particularly efficient though I wouldn't think - 
as you'd need to walk the whole range to find it's length.

r.retro.take(n).retro seems like the easiest fit.


More information about the Digitalmars-d mailing list