DMD 0.170 release

BCS BCS at pathlink.com
Wed Oct 18 09:45:06 PDT 2006


Frits van Bommel wrote:
> Walter Bright wrote:
> 
>> Hasan Aljudy wrote:
>>
>>> Also, since foreach already takes two arguments (int i, T t) that is, 
>>> index and element, you can add a third argument, of bool type, with 
>>> the meaning "start in reverse mode"
>>> foreach( true, i, d; "Hello" )
>>> {
>>>     writefln( d );
>>> }
>>>
>>> should print:
>>> o
>>> l
>>> l
>>> e
>>> H
>>
>>
>> That would work, and something like it has been suggested before, but 
>> it is too obscure looking. When someone sees "foreach_reverse", I 
>> think it'll be very clear what's going on.
> 
> 
> There's no need to use booleans (at least, not directly):
> 
> enum Ordering : bool { forward, backwards, reversed = backwards }
> 
> That should produce much more readable code, but work the same. Can be 
> done right now, except you can't overload opApply for arrays :(. (Tell 
> me if I'm wrong, but I tried and it didn't work)


IIRC something like this should work

<code>
int go(char[], int delegate(inout char [, ...]) dg)
{
}

void main()
{
	foreach(char c; &("hello world".go)){}
}
</code>


if not, this should work:


<code>
template reverse(T)
{
	int delegate(int delegate(inout T)) reverse(T[] stuff)
	{
		auto ret = new struct
		{
			T[] data;
			int go(int delegate(inout T) dg)
			{
				for (int i = data.length-1; i >= 0; i--)
				{
					if(int result = dg(data[i]);
						return result;
				}
				return 0;
			}
		}
		ret.data = stuff;
		return &ret.go;
	}
}


...

	foreach(char c; reverse("hello world"));
	{
		...
	}
</code>
// I havent tried either so I expect there to be a few errors in them.



More information about the Digitalmars-d-announce mailing list