array operations enhancements
KennyTM~
kennytm at gmail.com
Thu Aug 19 05:13:33 PDT 2010
On Aug 18, 10 14:41, F. Almeida wrote:
> It is an excellent feature of D2 that one can do
>
> double a[];
> double b[];
> double c[];
>
> //...
>
> c = a[] + 2.0*b[];
>
> But this is still limited, as we cannot include function calls in
> these operations.
>
> What if the compiler was able to introduce them in the assignment
> loop, provided that the functions pass ref double (in this case)?
>
> double anyfun(ref double x) { ... }
>
> c = anyfun(a[]) + 2.0*b[];
import std.algorithm;
import std.stdio;
import std.array;
auto vectorize(alias f, U)(U a) {
return array(map!f(a));
}
double square(double x) {
return x*x;
}
void main () {
auto a = [1.0, 2.0, 3.0, 4.0];
auto b = [5.0, 6.0, 7.0, 8.0];
auto c = [0.0, 0.0, 0.0, 0.0];
c[] = vectorize!square(a)[] + 2.0 * b[];
writeln(">", c);
}
More information about the Digitalmars-d
mailing list