Retrieving the traversed range

Simen kjaeraas simen.kjaras at gmail.com
Wed Aug 25 01:15:36 PDT 2010


Peter Alexander <peter.alexander.au at gmail.com> 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.

Does this do it?

reverse( array( until!pred( R ) ) );

Seeing as how you cannot use indexing, there is no lazy way to do this.

-- 
Simen


More information about the Digitalmars-d mailing list