d2: assigning ranges/iterators to array slices
Posted
by
modchan
on Stack Overflow
See other posts from Stack Overflow
or by modchan
Published on 2012-03-29T22:26:30Z
Indexed on
2012/03/29
23:28 UTC
Read the original article
Hit count: 268
Consider following code:
enum size = 16;
double[size] arr1 = [...];
double[size] arr2 = [...];
process = (double x) { return (x + 1); };
arr2[] = map!(process)(arr1[]); // here
I have trouble converting results of map
back to my plain array. Problem applies not only to map
, but also to take
, repeat
and all those fine tools from std.algorithm
and std.range
that operate on ranges.
On this assignment, I get Error: cannot implicitly convert expression (map(arr1[])) of type Result to double[]
. How can I evaluate range to array without using
uint i = 0;
foreach (x; map!(process)(arr1[])) {
arr2[i] = x;
i++;
}
?
Additionally, can someone please explain, why I must call map!(process)(arr1[])
instead of map!(process)(arr1)
with static arrays? Shouldn't static arrays be compatible with dynamic for means of iteration, or I don't get something?
Also, it seems that straightforward enumeration syntax foreach (index, item; sequence)
does not work for ranges - are there workarounds? I guess the reason is the same as why ranges cannot be assigned to array slices.
© Stack Overflow or respective owner