比较无法排序的可枚举值

时间:2019-11-06 14:14:49

标签: scala enumerable

我需要比较可枚举的值,但是这些值的集合不是 ordered

我有值Rock,Paper,Scissors,例如,我希望Rock输掉Paper。这没有发生,请参阅最后两行代码

object Move extends Enumeration {
    type Move = Value
    val Rock, Paper, Scissors = Value
}
import Move._

object MoveOrdering extends Ordering[Move] {
    def compare(m1: Move, m2: Move) = {
        (m1, m2) match {
            case (Rock, Paper) => -1
            case (Rock, Scissors) => +1
            case (Paper, Rock) => +1
            case (Paper, Scissors) => -1
            case (Scissors, Paper) => +1
            case (Scissors, Rock) => -1
            case _ => 0
        }
    }
}

Rock > Scissors // evaluates to false, I'd expect true
Scissors < Rock // evaluates to false, I'd expect true

我缺少什么使上面的代码 无法正常工作?

1 个答案:

答案 0 :(得分:4)

我认为您希望Rock > Scissors会考虑使用MoveOrdering

现在这就是它的工作方式,因为abstract class Value extends Ordered[Value]的默认实现为:

 abstract class Value extends Ordered[Value] with Serializable {
    /** the id and bit location of this enumeration value */
    def id: Int
    /** a marker so we can tell whose values belong to whom come reflective-naming time */
    private[Enumeration] val outerEnum = thisenum

    override def compare(that: Value): Int =
      if (this.id < that.id) -1
      else if (this.id == that.id) 0
      else 1

因此,默认情况下,它使用枚举中定义的元素的index。根据您的情况,默认为Rock < Paper < Scissors。如果您希望使用自定义顺序:

object Move extends Enumeration{
  type Move = Value
  val Paper, Scissors, Rock = Value
}
import Move._
implicit object MoveOrdering extends Ordering[Move] {
  def compare(m1: Move, m2: Move) = {
    (m1, m2) match {
      case (Rock, Paper) => -1
      case (Rock, Scissors) => +1
      case (Paper, Rock) => +1
      case (Paper, Scissors) => -1
      case (Scissors, Paper) => +1
      case (Scissors, Rock) => -1
      case _ => 0
    }
  }
}

implicit val o = implicitly[Ordering[Move]]

println(List(Rock, Scissors, Paper).sorted)
println(o.lt(Rock, Scissors))