Retrieving the traversed range

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Aug 25 06:10:18 PDT 2010


On 8/25/10 1:08 PDT, Peter Alexander wrote:
> Maybe I'm missing something, but I can't think of anyway *at all* to do
> this generically.
>
> Lets say I have some arbitrary bidirectional range, R, and I want to
> find the first element that satisfies some predicate. After that, I want
> to reverse the part of the range up to that element.
>
> Essentially, I'd like to do something along the lines of:
>
> reverse(until!(pred)(R));
>
> but Until is (correctly) not bidirectional, so I can't do that.
>
> Use indexing and slicing is not an option because the range isn't
> necessary random-access.
>
> This isn't about what std.algorithm and std.range can do -- I can't even
> think of a way to do this using primitive range operations.
>
> Also note that popping off from the back of the range is not an option
> either because the range could be arbitrarily long and the predicate is
> usually satisfied very early in the range.
>
> Thanks in advance.

This is an annoying limitation of bidirectional ranges that we don't 
have a solid solution to. Let me put on the table the unacceptable 
solution as a baseline:

auto n = walkLength(r) - walkLength(until!pred(r));
popBackN(r, n);
reverse(r);

Of all ranges, bidirectional ranges suffer of this limitation because 
they clearly have two underlying "ends" that are inaccessible in 
separation from the range.


Andrei


More information about the Digitalmars-d mailing list