They are not the same

Meta jared771 at gmail.com
Fri Apr 4 18:57:57 PDT 2014


On Saturday, 5 April 2014 at 01:28:06 UTC, bearophile wrote:
> 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

When I put a println inside both functions, they both print out 
[2, 1, 0]. Shouldn't the first print [0, 1, 2] while the second 
prints [2, 1, 0]?


More information about the Digitalmars-d-learn mailing list