[Q] Array not Comparable?

W

Warren Brown

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
 
E

Emmanuel Touzery

Warren said:
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
 
T

ts

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
 
G

Gavin Sinclair

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
 
T

ts

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
 
G

Gavin Sinclair

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
 
T

ts

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
 
S

Simon Strandgaard

"Warren Brown" <[email protected]> skrev i en meddelelse
[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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,138
Messages
2,570,805
Members
47,349
Latest member
jojonoy597

Latest Threads

Top