Использование сопоставления с образцом для Ordered.compare в Scala - PullRequest
2 голосов
/ 19 апреля 2011

У меня есть следующий класс Scala:

case class Person(firstName: String, lastName: String, age: Int)
    extends Ordered[Person] {
  def compare(that: Person): Int = {
    if (this.lastName < that.lastName) -1
    else if (this.lastName > that.lastName) 1
    else if (this.firstName < that.firstName) -1
    else if (this.firstName > that.firstName) 1
    else this.age compare that.age
  }
}

, чтобы разрешить сортировку по фамилии, имени и возрасту.

Как я могу написать это, используя сопоставление с образцом? Я придумал следующее, но есть ли лучший способ?

case class Person(firstName: String, lastName: String, age: Int)
    extends Ordered[Person] {
  def compare(that: Person): Int = {
    that match {
      case Person(_, thatLastName, _) if this.lastName < thatFile => -1
      case Person(_, thatLastName, _) if this.lastName > thatFile => 1

      case Person(thatFirstName, _, _) if this.firstName < thatFirstName => -1
      case Person(thatFirstName, _, _) if this.firstName > thatFirstName => 1

      case Person(_, _, thatAge) => this.age compare thatAge
    }
  }
}

ОБНОВЛЕНИЕ : изменено на Ordering[A] согласно ответу Ландея:

implicit val personOrdering = new Ordering[Person] {
  def compare(first: Person, second:Person): Int = {
    second match {
      case Person(_, thatLastName, _) if first.lastName < thatLastName => -1
      case Person(_, thatLastName, _) if first.lastName > thatLastName => 1

      case Person(thatFirstName, _, _) if first.firstName < thatFirstName => -1
      case Person(thatFirstName, _, _) if first.firstName > thatFirstName => 1

      case Person(_, _, thatAge) => first.age compare thatAge
    }
  }
}

case class Person(firstName: String, lastName: String, age: Int)

но кажется неловким, что я сопоставляю только second. Как я могу сделать его более "элегантным"?

1 Ответ

10 голосов
/ 19 апреля 2011

Предпочтительным способом в Scala является предоставление неявного упорядочения вместо упорядоченного, что гораздо более гибко и не вызывает головной боли в отношении наследования.

Что касается сопоставления с образцом, я не вижу лучшего способа, потому что результатом методов сравнения являются Int s, которые не гарантируют -1, 0, 1. Решение Haskell для возврата "enum" объектов (LT , EQ, GT) намного чище и сопоставим с шаблоном, но, похоже, Scala следовала здесь традиции C ++ / Java по соображениям совместимости.

Конечно, вы можете развернуть свой собственный "каркас" сравнения:

abstract sealed class CompResult(val toInt:Int) {
  def andThen(next: => CompResult): CompResult
}
case object LT extends CompResult(-1) {
 def andThen(next: => CompResult) = LT
}
case object EQ extends CompResult(0) {
  def andThen(next: => CompResult) = next
}
case object GT extends CompResult(1) {
  def andThen(next: => CompResult) = GT
}

implicit def int2Comp(n:Int) =
   if (n == 0) EQ else if (n < 0) LT else GT


(("sdkfhs" compareTo "fldgkjdfl"):CompResult) match {
  case LT => println("less")
  case EQ => println("same")
  case GT => println("more")
}

В вашем случае вы могли бы написать:

case class Person(firstName: String, lastName: String, age: Int)
  extends Ordered[Person] {
  def compare(that: Person): Int = {
    (this.lastName compareTo that.lastName).
    andThen (this.firstName compareTo that.firstName).
    andThen (this.age compare that.age).toInt
  }
}
...