The rfind challenge

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Jan 15 09:14:29 PST 2013


On 1/15/13 12:03 PM, Phil Lavoie wrote:
> Pseudo:
> original = range.save
> resPos = range.retro.find
> return Range.merge( resPos, original ) //DOES NOT WORK
>
> The problem with this solution is that:
> Retro returns a different range type and it would be more complicated to
> have the merge function work with this type.

This would be difficult to implement safely, which is an important 
disadvantage.

> An addition to this solution could be another new primitive: reverse:
>
> Pseudo:
> original = range.save
> reversed = range.reverse //Permanently invert start an end. I think this
> is feasible for all bidirectional ranges. Still a bidirectional range.
> reversed.find( needle ) //In haystack :)
> return Range.merge( reversed, original ) //Ok, start of reversed is on
> needle and end of original hasn't moved. The order of the range returned
> is the same as the one received.
>
> This is just a suggestion and any primitive could be renamed. However, I
> think reverse is a good place to start.

Reverse is really cool and immediate to implement safely for a lot of 
ranges I can think of. How would you define rfind assuming reverse?


Andrei


More information about the Digitalmars-d mailing list