The rfind challenge

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


On Tue, Jan 15, 2013 at 03:59:17PM -0500, Andrei Alexandrescu wrote:
> On 1/15/13 2:07 PM, Phil Lavoie wrote:
> >Continuing with reversible ranges:
> 
> I don't think .reverse will take us far enough. Won't work with
> arrays which kinda puts a monkey wrench into everything.
[...]

Not really. We can define an array wrapper with a .reverse that returns
the original array, something like:

	// In std.array
	auto reverse(R)(R array)
		if (is(R _ : E[], E)
	{
		static struct ReverseArray {
			R[] src;

			@property auto front() { return src[$-1]; }
			@property bool empty() { return src.empty; }
			@property void popFront() {
				src = src[0..$-1];
			}
			@property auto reverse() {
				return src;
			}
			... // other wrapper methods
		}
		return ReverseArray(array);
	}

This will let you implement rfind in a way that returns the original
array.


T

-- 
Not all rumours are as misleading as this one.


More information about the Digitalmars-d mailing list