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
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