[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[Q] Array not Comparable?

Warren Brown

10/21/2003 10:41:00 PM

In the past I have sorted arrays of arrays and so I knew that Array
implemented the spaceship operator (<=>), since sort uses that operator
to perform the sort. I also knew that the Comparable mixin requires
only that a class implement the spaceship operator. Somewhere in there,
I guess I just assumed that Array included Comparable, but apparently it
doesn't.

This came up in a routine where I was trying to find the "best"
element in a list. There were three qualities that I was using to
determine "bestness": quality1, quality2, and quality3. Eventually I
got down to the line where I need to decide if the current word is
"better" than my current "best". The condition started to look like:

if (quality1 > maxquality1) ||
((quality1 == maxquality1) && (quality2 > maxquality2)) ||
((quality1 == maxquality1) && (quality2 == maxquality2) &&
(quality3 > maxquality3))

I realized that I could simplify this entire mess into something
clear, concise, and expandable (adding new qualities) by using arrays:

if [quality1,quality2,quality3] > [maxquality1,maxquality2,maxquality3]

However, since Array does not include Comparable, this throws an
error. Now, I can open the Array class myself and include Comparable
and this begins working just fine. Alternately, I could invoke the
spaceship operator instead of the less than operator and check for a
result of 1. But neither of these is as elegant as the original.

So, my questions are:

1) Is there a reason for Array to NOT include Comparable even though
the spaceship operator is defined?
2) If not, can this be included in future versions?
3) If so, do I need to submit an RCR?
4) Is there some other concise way to achieve what I am trying to
do?


Thanks in advance,

- Warren Brown



8 Answers

Emmanuel Touzery

10/22/2003 1:40:00 PM

0

Warren Brown wrote:

> So, my questions are:
>
> 1) Is there a reason for Array to NOT include Comparable even though
>the spaceship operator is defined?
> 2) If not, can this be included in future versions?
> 3) If so, do I need to submit an RCR?
> 4) Is there some other concise way to achieve what I am trying to
>do?
>
>
i'm surprised nobody answers this. a quick google doesn't find anything
on this. i find this very interesting too..

emmanuel



ts

10/22/2003 1:49:00 PM

0

>>>>> "E" == Emmanuel Touzery <emmanuel.touzery@wanadoo.fr> writes:

E> i'm surprised nobody answers this. a quick google doesn't find anything
E> on this. i find this very interesting too..

[ruby-talk:22363]


Guy Decoux





Gavin Sinclair

10/22/2003 2:05:00 PM

0

On Wednesday, October 22, 2003, 11:49:17 PM, ts wrote:

>>>>>> "E" == Emmanuel Touzery <emmanuel.touzery@wanadoo.fr> writes:

E>> i'm surprised nobody answers this. a quick google doesn't find anything
E>> on this. i find this very interesting too..

> [ruby-talk:22363]


Quoting from that ref:

> |Good. By the way, when will Array include Comparable ?
>
> They won't. Arrays are not comparable in general. They are
> comparable only when all of their elements are comparable.
>
> matz.


A. "Arrays are not comparable in general."
B. Array implements <=>.

Seems like a contradiction to me.

Gavin


ts

10/22/2003 2:41:00 PM

0

>>>>> "G" == Gavin Sinclair <gsinclair@soyabean.com.au> writes:

G> A. "Arrays are not comparable in general."
G> B. Array implements <=>.

G> Seems like a contradiction to me.

Well, if you look at the classes which include Comparable you'll see that
#<=> return (-1, 0, 1) when the 2 objects are in the same class (this is
simplified, see for example String#<=>)

This is not the case for Array

svg% ruby -e 'p ([1, "a"] <=> [1, 2])'
nil
svg%

You have 2 arrays, but ruby can't compare these objects


Guy Decoux



Gavin Sinclair

10/22/2003 2:47:00 PM

0

On Thursday, October 23, 2003, 12:40:50 AM, ts wrote:

>>>>>> "G" == Gavin Sinclair <gsinclair@soyabean.com.au> writes:

G>> A. "Arrays are not comparable in general."
G>> B. Array implements <=>.

G>> Seems like a contradiction to me.

> Well, if you look at the classes which include Comparable you'll see that
> #<=> return (-1, 0, 1) when the 2 objects are in the same class (this is
> simplified, see for example String#<=>)

> This is not the case for Array

> svg% ruby -e 'p ([1, "a"] <=> [1, 2])'
> nil
> svg%

> You have 2 arrays, but ruby can't compare these objects


So:

class Array
include Comparable
end

[1, 3] > [1, 2] # true
[1, "a"] > [1, 2] # ArgumentError: comparison of Array with Array failed


Seems logical to me.

Gavin


ts

10/22/2003 2:53:00 PM

0

>>>>> "G" == Gavin Sinclair <gsinclair@soyabean.com.au> writes:

G> Seems logical to me.

You find logical to say that 2 arrays are comparable (this is, in sort,
what you say when you include Comparable) and then give an error
to say that it's not possible to compare 2 arrays ?

You must be an "anglois" :-)


Guy Decoux



Melanie Fielder

10/22/2003 4:05:00 PM

0


"Warren Brown" <wkb@airmail.net> skrev i en meddelelse
news:000201c39824$58f435b0$4d4da8c0@home...
[snip]
> This came up in a routine where I was trying to find the "best"
> element in a list. There were three qualities that I was using to
> determine "bestness": quality1, quality2, and quality3. Eventually I
> got down to the line where I need to decide if the current word is
> "better" than my current "best". The condition started to look like:
[snip]
> 4) Is there some other concise way to achieve what I am trying to
> do?


Why not use Array#sort to identify the best element ?

Like this. A subarray consists of the 3 qualities.

irb(main):001:0> q = [[3, 2, 1], [3, 1, 2], [2, 3, 1], [2, 1, 3]]
=> [[3, 2, 1], [3, 1, 2], [2, 3, 1], [2, 1, 3]]
irb(main):002:0> q.sort
=> [[2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
irb(main):003:0>

--
Simon Strandgaard


}:-\)[o[

8/13/2009 7:28:00 PM

0


L'info vient du SPLC, un organisme sans aucune cr?dibilit? qui est
reconnu pour crier au loup

============

ca te ressemble bcp