The rfind challenge

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Jan 15 05:13:43 PST 2013


On 1/15/13 6:53 AM, FG wrote:
> On 2013-01-15 06:51, Andrei Alexandrescu wrote:
>> If r is a forward range but not better, this is rather simple:
>>
>> R rfind(R, E)(R range, E element)
>> {
>> for (;;)
>> {
>> auto ahead = range.save.find(element);
>> if (ahead.empty) return range;
>> range = ahead;
>> }
>> }
>
> That example creates an infinite loop. Needs a popFront:
>
> R rfind(R, E)(R range, E element)
> {
> if (range.empty) return range;
> for (;;)
> {
> auto ahead = range.save;
> ahead.popFront();
> ahead = ahead.find(element);
> if (ahead.empty) return range;
> range = ahead;
> }
> }
>
> Another thing: if the element is not found, I think an empty range
> should be returned and not the whole range.

Thanks for the fixes!

Andrei


More information about the Digitalmars-d mailing list