[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Changing ==,>,<, etc

Marcin 'Qrczak' Kowalczyk

9/17/2003 12:38:00 PM

Dnia czw 31. lipca 2003 22:03, Martin DeMello napisa3:

> any? and all? are very receiver-centric - compare
>
> a.any? {|i| b.all? {|j| i < j} }
>
> with
>
> any(a) < all(b)
>
> The latter shows up the symmetry a lot more clearly - I've always hated
> having to write nested loops to express something flat.

But there is no symmetry. Does it mean that there exists something in "a"
such that it's smaller than everything in "b", or that for everything in
"b" there exists a smaller thing in "a"?

Well, for "<" it happens to be equivalent because "<" is transitive; it's
the same as a.min < b.min for non-empty "a" and "b". What about "!="?
Does any(1,2) != all(1,2)?

--
__("< Marcin Kowalczyk
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.p...


3 Answers

Martin DeMello

9/17/2003 12:50:00 PM

0

Marcin ''Qrczak'' Kowalczyk <qrczak@knm.org.pl> wrote:
> Dnia czw 31. lipca 2003 22:03, Martin DeMello napisa?:
>
>> any? and all? are very receiver-centric - compare
>>
>> a.any? {|i| b.all? {|j| i < j} }
>>
>> with
>>
>> any(a) < all(b)
>>
>> The latter shows up the symmetry a lot more clearly - I''ve always hated
>> having to write nested loops to express something flat.
>
> But there is no symmetry. Does it mean that there exists something in "a"
> such that it''s smaller than everything in "b", or that for everything in
> "b" there exists a smaller thing in "a"?
>
> Well, for "<" it happens to be equivalent because "<" is transitive; it''s
> the same as a.min < b.min for non-empty "a" and "b". What about "!="?
> Does any(1,2) != all(1,2)?

No - this unrolls to

[1,2].any? {|i| [1,2].all? {|j| i != j}}

which is false.

The symmetry is highlighted by the other way to unroll it:

[1,2].all? {|j| [1,2].any? {|i| i != j}}

i.e., there is no reason the LHS should be in the outer loop, rather
than the RHS. My point was that the only reason there *was* an ''outer
loop'' was that the language forces you to write loops nestedly, whereas
a cross product (essentially what this is) is flat.

The only asymmetry involved is the fact that you have to keep the loop
variables in their proper order with respect to the operator, function
or whatever (not evident here because != is commutative).

Also, any(1,2) != any(1,2) should be true.

martin

Marcin 'Qrczak' Kowalczyk

9/17/2003 4:06:00 PM

0

Dnia ?ro 17. wrze?nia 2003 14:54, Martin DeMello napisa3:

> i.e., there is no reason the LHS should be in the outer loop, rather
> than the RHS. My point was that the only reason there *was* an ''outer
> loop'' was that the language forces you to write loops nestedly, whereas
> a cross product (essentially what this is) is flat.

I disagree. It''s essential which is outer and which is inner unless
quantifiers happen to commute (both "any" or both "all"). The decision to
put LHS in the outer loop is arbitrary; if you wanted the other way you
must search for an operator with flipped arguments (which happens to be
easy among the six comparison operators).

> Also, any(1,2) != any(1,2) should be true.

I wrote any(1,2) != all(1,2). And what about all(1,2) != any(1,2)?
If they are the same, then "any" and "all" must be treated with different
priority. If they are different, then != is no longer commutative.
(I suspect the latter.) Both cases are unintuitive for me.

--
__("< Marcin Kowalczyk
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.p...


Martin DeMello

9/18/2003 7:13:00 AM

0

Marcin ''Qrczak'' Kowalczyk <qrczak@knm.org.pl> wrote:
> Dnia ?ro 17. wrze?nia 2003 14:54, Martin DeMello napisa?:
>
>> i.e., there is no reason the LHS should be in the outer loop, rather
>> than the RHS. My point was that the only reason there *was* an ''outer
>> loop'' was that the language forces you to write loops nestedly, whereas
>> a cross product (essentially what this is) is flat.
>
> I disagree. It''s essential which is outer and which is inner unless
> quantifiers happen to commute (both "any" or both "all"). The decision to
> put LHS in the outer loop is arbitrary; if you wanted the other way you
> must search for an operator with flipped arguments (which happens to be
> easy among the six comparison operators).
>
>> Also, any(1,2) != any(1,2) should be true.
>
> I wrote any(1,2) != all(1,2). And what about all(1,2) != any(1,2)?
> If they are the same, then "any" and "all" must be treated with different
> priority. If they are different, then != is no longer commutative.
> (I suspect the latter.) Both cases are unintuitive for me.

Good point. I see now that it''s analogous to the fact that

(forall x)(there exists y)(x != y)

is different from

(there exists y)(forall x)(x != y)

So there is, indeed, as you pointed out, the notion of an inner and an
outer quantifier.

martin