On Thursday, 23 August 2012 at 00:19:39 UTC, bearophile wrote:
At page 69 of those slides there is some code that looks interesting, I think this is a reduced version of part of it, that shows another way to use vectorized comparisons:


void main() {
    double[] a = [1.0, 1.0, -1.0, 1.0, 0.0, -1.0];
    double[] b = [10,   20,   30,  40,  50,   60];
    double[] c = [1,     2,    3,   4,   5,    6];
    if (a[] > 0)
        b[] += c[];
}


I think that code is semantically equivalent to:

void main() {
    double[] a = [1.0, 1.0, -1.0, 1.0, 0.0, -1.0];
    double[] b = [10,   20,   30,  40,  50,   60];
    double[] c = [1,     2,    3,   4,   5,    6];
    foreach (i; 0 .. a.length)
        if (a[i] > 0)
            b[i] += c[i];
}

The proposed syntax looks weird. Wouldn't the followind be more intuitive:

foreach (i; a[i] > 0)
    b[i] += c[i];

Or alternatively it would be nice to be able to do it like in Matlab:

i = (a[] > 0);
b[i] += c[i];

Reply via email to