void main() {
auto a = [0, 2, 0];
auto b = [1, 1, 1];
// add 2 to ALL elements in C
auto c = a.dup;
c[] += 2;
assert(c == [2, 4, 2]);
// add ALL elements in B to D
auto d = a.dup;
d[] += b[];
assert(d == [1, 3, 1]);
// checks ALL elements in A are less than B
assert(!(a < b)); // Error ...
}
As is shown above, there is an inconsistency in how array element-wise comparisons are handled in D.
The logical conclusion (IMHO) after using the array vector operations that mutated all elements in the lhs, is that the operation would check that the comparison was true for (again) **ALL** elements of the arrays.
Instead, it appears to only check for the first element for which the predicate holds true, and then stops.
This is really weird, especially when the following holds:
assert([2, 0, 0] > [1, 1, 1]);
assert([0, 2, 0] < [1, 1, 1]);
assert([0, 0, 2] < [1, 1, 1]);
I couldn't find any relevant documentation on how this was supposed to work, so these are just best guesses support with data.
Comment #1 by ibuclaw — 2013-11-01T03:32:37Z
Your assumption is not quite right. This is the loop comparisons goes off:
for (size_t u = 0; u < len; u++)
{
int result = s1[u] - s2[u];
if (result)
return result;
}
And in you examples:
---
Code: assert([2, 0, 0] > [1, 1, 1]);
---
Generates: assert(compare(s1, s2) > 0);
---
Returns: result = 2 - 1; => return 1
---
Code: assert([0, 2, 0] < [1, 1, 1]);
---
Generates: assert(compare(s1, s2) < 0);
---
Returns: result = 0 - 1; => return -1;
---
Code: assert([0, 0, 2] < [1, 1, 1]);
---
Generates: assert(compare(s1, s2) < 0);
---
Returns: result = 0 - 1; => return -1;
My advise to you would be to compare a *SORTED* array.
Marking as invalid because this is working as expected.
Comment #2 by daniel350 — 2013-11-01T03:44:39Z
(In reply to comment #1)
> Your assumption is not quite right. This is the loop comparisons goes off:
>
> for (size_t u = 0; u < len; u++)
> {
> int result = s1[u] - s2[u];
> if (result)
> return result;
> }
>
>
> And in you examples:
> ---
> Code: assert([2, 0, 0] > [1, 1, 1]);
> ---
> Generates: assert(compare(s1, s2) > 0);
> ---
> Returns: result = 2 - 1; => return 1
>
> ---
> Code: assert([0, 2, 0] < [1, 1, 1]);
> ---
> Generates: assert(compare(s1, s2) < 0);
> ---
> Returns: result = 0 - 1; => return -1;
>
> ---
> Code: assert([0, 0, 2] < [1, 1, 1]);
> ---
> Generates: assert(compare(s1, s2) < 0);
> ---
> Returns: result = 0 - 1; => return -1;
>
>
>
> My advise to you would be to compare a *SORTED* array.
>
> Marking as invalid because this is working as expected.
Why would I want to compare a sorted array?
I want to compare two different arrays?
I see no reason why a built-in comparison would assume the array would be sorted?
This is working as it is implemented, but I wouldn't say it is working as expected. Not by a long shot.
Is there any reference for this behaviour?
Comment #3 by daniel350 — 2013-11-01T03:56:03Z
(In reply to comment #1)
> Your assumption is not quite right. This is the loop comparisons goes off:
>
> for (size_t u = 0; u < len; u++)
> {
> int result = s1[u] - s2[u];
> if (result)
> return result;
> }
I understand that is more or less what the loop was going off, and I am saying, that is inconsistent behaviour.
It also makes no sense as a comparison, because as long as the arrays aren't equal, it will always only compare the first element!?
What the?
What kind of comparison is that for a set of operations that is on 'arrays', not sorted data structures.
Comment #4 by ibuclaw — 2013-11-01T04:16:51Z
(In reply to comment #3)
> (In reply to comment #1)
> > Your assumption is not quite right. This is the loop comparisons goes off:
> >
> > for (size_t u = 0; u < len; u++)
> > {
> > int result = s1[u] - s2[u];
> > if (result)
> > return result;
> > }
>
> I understand that is more or less what the loop was going off, and I am saying,
> that is inconsistent behaviour.
> It also makes no sense as a comparison, because as long as the arrays aren't
> equal, it will always only compare the first element!?
>
> What the?
> What kind of comparison is that for a set of operations that is on 'arrays',
> not sorted data structures.
Take by way of example, how you compare two words. You'd say that betty comes before hello, thus "betty" < "hello" is true.
In the same logic, [0,2,0] comes before [1,1,1], thus [0,2,0] < [1,1,1].