This is derived from part of a D.learn post, maybe it's of your interest.

This program shows two similar versions of the same loop:


import std.stdio, std.algorithm, std.typetuple, std.range;

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

    foreach (_; 0 .. 2) {
        foreach (sub; items) {
            iota(sub.length)
            .map!((i){ writeln(sub); return 0; })
            .array();
        }
    }

    writeln();

    foreach (_; TypeTuple!(0, 1)) {
        foreach (sub; items) {
            iota(sub.length)
            .map!((i){ writeln(sub); return 0; })
            .array();
        }
    }
}


Its output:

[10, 20]
[10, 20]
[30]
[10, 20]
[10, 20]
[30]

[10, 20]
[10, 20]
[30]
[30]
[30]
[30]


Do you see why the output of the two foreach(_) is different?

Bye,
bearophile

Reply via email to