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.