Jump to page: 1 2
Thread overview
Array operation with boolean operator
Mar 14, 2013
n00b
Mar 14, 2013
John Colvin
Mar 14, 2013
bearophile
Mar 14, 2013
bearophile
Mar 14, 2013
n00b
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
Ali Çehreli
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
bearophile
Mar 14, 2013
Andrea Fontana
Mar 14, 2013
bearophile
Mar 15, 2013
Marco Leise
Mar 15, 2013
bearophile
March 14, 2013
I tried to use a boolean operator for an array operation :

a[] = b[] < c[];

It compiles but seems to only fill a[] with the result of b[0] < c[0].
Is there any "rational" reason to that?
And is there any way to use boolean operator for array operations?
March 14, 2013
On Thursday, 14 March 2013 at 13:58:45 UTC, n00b wrote:
> I tried to use a boolean operator for an array operation :
>
> a[] = b[] < c[];
>
> It compiles but seems to only fill a[] with the result of b[0] < c[0].
> Is there any "rational" reason to that?
> And is there any way to use boolean operator for array operations?

As far as I can tell, this is a bug.
March 14, 2013
n00b:

> Is there any "rational" reason to that?

It's not implemented (and it's a bug that it returns something different). Take a look in Bugzilla if it's already there.


> And is there any way to use boolean operator for array operations?

I think the only supported boolean vec operation is assignment.

Bye,
bearophile
March 14, 2013
> Take a look in Bugzilla if it's already there.

Code for a bug report:


import core.stdc.stdio: printf;
void main() {
    int[3] a = [1, 5, 1];
    int[3] b = [1, 1, 5];
    int[3] c;
    c[] = a[] < b[];
    printf("%d %d %d", c[0], c[1], c[2]);
}


Output:
0 0 0


Expected output:
0 0 1

Bye,
bearophile
March 14, 2013
This has already been reported
http://d.puremagic.com/issues/show_bug.cgi?id=5636
March 14, 2013
On Thursday, 14 March 2013 at 13:58:45 UTC, n00b wrote:
> I tried to use a boolean operator for an array operation :
>
> a[] = b[] < c[];
>
> It compiles but seems to only fill a[] with the result of b[0] < c[0].
> Is there any "rational" reason to that?
> And is there any way to use boolean operator for array operations?

Maybe this way:

int a[] = [1,2,3];
int b[] = [3,2,1];

a.zip(b).map!"a[0]<a[1]".writeln;
return;
March 14, 2013
On Thursday, 14 March 2013 at 13:58:45 UTC, n00b wrote:
> I tried to use a boolean operator for an array operation :
>
> a[] = b[] < c[];
>
> It compiles but seems to only fill a[] with the result of b[0] < c[0].
> Is there any "rational" reason to that?

Yes i think there is a rational reason. Check this:

int a[] = [1,0,0];
int b[] = [0,1,0];
int c[] = [1,1,0];

bool ab = a[] < b[]; // False.  a[0] > b[0]
bool ac = a[] < c[]; // True.  a[0] == c[0] but a[1] < c[1];

writeln(ab);
writeln(ac);
c[] = ab; // <-- assign!

You see? Your code do this:

a[] = (b[] < c[]);
March 14, 2013
On 03/14/2013 10:45 AM, Andrea Fontana wrote:

> You see? Your code do this:
>
> a[] = (b[] < c[]);

I thought the same thing at first but note the brackets after b and c. Those should make this an array-wise operation.

For all elements of a to be the same value, one would not write the brackets:

    a[] = b < c;

Some of the array-wise operations are confusing. :/

Ali

March 14, 2013
On Thursday, 14 March 2013 at 17:56:07 UTC, Ali Çehreli wrote:
> On 03/14/2013 10:45 AM, Andrea Fontana wrote:
>
> > You see? Your code do this:
> >
> > a[] = (b[] < c[]);
>
> I thought the same thing at first but note the brackets after b and c. Those should make this an array-wise operation.
>
> For all elements of a to be the same value, one would not write the brackets:
>
>     a[] = b < c;
>
> Some of the array-wise operations are confusing. :/
>
> Ali

Doesn't a[] means copy of a?
So copyofa[] < copyofb[] == bool

I don't think a boolean operator can be array-wise... I always think X < Y return a single bool, indipendently from X or Y type (class, array or what else). Math operations is a different topic in my mind.
March 14, 2013
Andrea Fontana:

> I always think X < Y return a single bool,

Having a practice with NumPy and the like, I think X < Y as returning as many bools as the length of X and Y:

>>> from numpy import *
>>> a = array([1, 5, 1])
>>> b = array([1, 1, 5])
>>> a < b
array([False, False,  True], dtype=bool)


Programmers with experience of Matlab, Matcad, etc, feel the same.

Bye,
bearophile
« First   ‹ Prev
1 2