Initializing a complex dynamic array (with real part from one array, and imaginary from other array)?
Ali Çehreli
acehreli at yahoo.com
Thu Jul 22 07:59:24 UTC 2021
On 7/21/21 2:17 AM, drug wrote:
> auto z = zip(x, y) // concatenate two ranges
> .map!(a=>Complex!double(a[0],a[1])) // take the current first
> element of the first range as the real part and the current first
> element of the second range as the imaginary part
> .array; // convert the lazy range
> to a dynamic array, probably you can avoid but this depends on how you
> use it later
One of the *nonexistent* ;) features of D is automatic tuple expansion,
which works only in foreach loops. I think it makes the code much more
readable in this case:
Complex!double[] z;
import std.range;
import std.algorithm;
foreach (re, im; zip(x, y)) {
z ~= complex(re, im);
}
An alternative is lockstep:
foreach (re, im; lockstep(x, y)) {
z ~= complex(re, im);
}
Ali
More information about the Digitalmars-d-learn
mailing list