They are not the same

John Colvin john.loughran.colvin at gmail.com
Sat Apr 5 02:27:44 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

Pity...

I think there's an argument that this should work, on the grounds 
that the context pointer is just another argument and therefore 
the lambda can be weakly pure.


More information about the Digitalmars-d-learn mailing list