Am 12.08.2011, 15:08 Uhr, schrieb bearophile <[email protected]>:

Marco Leise:

Just remember that reverse loops are written like this:

for (size_t i = x.length; i-- > 0; ) {...}

Thankfully in D there is foreach_reverse :-)

import std.stdio;

void main() {
    auto array = [10, 20, 30];

    for (size_t i = array.length; i-- > 0; )
        writeln(i, " ", array[i]);

    foreach_reverse (i, item; array)
        writeln(i, " ", item);
}

Bye,
bearophile

Sure, but we were talking about for, not foreach. Maybe these cases in Phobos cannot use foreach, because they modify 'i' or they don't require 'item'.

Reply via email to