They are not the same

bearophile bearophileHUGS at lycos.com
Fri Apr 4 18:28:05 PDT 2014


Can you spot the difference between foo1 and foo2?


import std.algorithm: map;
import std.range: iota;

void foo1(in int[] a, in int[] b) pure {
     int[] r;
     foreach (immutable i; 0 .. a.length)
         r ~= (i % 2) ? a[i] : b[i];
}

void foo2(in int[] a, in int[] b) pure {
     int[] r;
     foreach (x; iota(a.length)
                 .map!(i => (i % 2) ? a[i] : b[i]))
         r ~= x;
}

void main() {}



Sometimes variants of this problem hit me. I don't even know if 
this simple problem has a name. Is it impossible to solve?

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list