The rfind challenge

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 15 10:13:40 PST 2013


On Tue, Jan 15, 2013 at 07:54:12AM -0500, Andrei Alexandrescu wrote:
> On 1/15/13 1:31 AM, H. S. Teoh wrote:
> >Hmm. What about introducing a new primitive takeUntil, that returns the
> >initial segment of the range until a given predicate becomes true? Then
> >you could implement rfind thus:
> >
> >	auto rfind(alias pred, R)(R range)
> >		if (isBidirectionalRange!R)
> >	{
> >		return range.retro()
> >			.takeUntil!pred()
> >			.retro();
> >	}
> 
> That works, but returns a different type. Ideally rfind would return
> the same type as the initial range, R.
[...]

Hmm. Then it is indeed impossible with the current primitives, because
to iterate from the back, one would have to pop off the back, so by
definition you can't reach the back anymore.


T

-- 
Государство делает вид, что платит нам зарплату, а мы делаем вид, что работаем.


More information about the Digitalmars-d mailing list