DMD 0.170 release
Oskar Linde
oskar.lindeREM at OVEgmail.com
Wed Oct 18 06:23:13 PDT 2006
Bill Baxter wrote:
> Oskar Linde wrote:
>> Walter Bright wrote:
>>
>>> Bill Baxter wrote:
>>>
>>>> I don't see how it helps. If you can already do:
>>>> foreach(T item; &collection.reversed()) { }
>>>
>>>
>>> That doesn't work for arrays.
>>
>>
>> In what way does it not work? I have been doing:
>>
>> foreach(x; "abcd".reverseView())
>> writef("%s",x);
>>
>> prints "dcba"
>>
>> For any type of built in array for a very long time (long before
>> 0.170), and it certainly seems to work for me.
>
> How do you do that? I just get
> : undefined identifier reverseView
> : function expected before (), not reverseView of type int
> : cannot infer type for x
Sorry. I wasn't very clear on this in my post. Here is a simple
implementation that runs:
struct ReverseIterator(T:T[]) {
T[] array;
int opApply(int delegate(inout T) dg) {
for (int i = array.length-1; i >= 0; i--) {
if (auto status = dg(array[i]))
return status;
}
return 0;
}
}
ReverseIterator!(Array) reverseView(Array)(Array array) {
ReverseIterator!(Array) iter; // = {array} => ICE
iter.array = array;
return iter;
}
import std.stdio;
void main() {
foreach(x; "abcd".reverseView())
writef("%s",x);
}
More information about the Digitalmars-d-announce
mailing list