scala 如何对一个密封的特征进行排序?

dphi5xsq  于 2023-06-06  发布在  Scala
关注(0)|答案(4)|浏览(203)

我有一个由某种“状态机”(“流程图”)定义的分布式系统
每个系统将其状态写入共享“日志”中
我将每个状态表示为密封特性的一部分以及该状态的给定“状态”
我想“合并/减少”到一个单一的状态,代表当前的进展。
(有一些放松的地方,因为不是所有的必须成功,以便最终状态成功完成)
有2个密封的特征代表流:

sealed trait System
case object A extends System
case object B extends System
case object C extends System
...

sealed trait Status
case object Pending extends Status
case object InProgress extends Status
case object Success extends Status
case object Fail extends Status

日志:

A, Success
B, Fail
C, Pending
...
...

现在有一组规则,我使用它们来定义单个状态降低
基本上它会优先考虑
A < B < C, ... < Z
和/或
Pending < InProgress < Success < Fail
因此,如果存在以下状态:
(A, Success)(C, Pending)
我想把它简化为(C,Pending)
如果
(A,Success)(B, Fail)
我想把它缩小到(B, Fail)
在我的例子中,我可以将其建模为一个简单的整数比较(可能带有一个我显式测试的异常值)
我不清楚如何使密封的特征可比较/可排序,这将使我的生活方式更容易
沿着这些思路做就足够了:

def reduce(states: Seq[(System,Status)]) : (System,Status) = {
    states.order... {left.system < right.system) && (a.status < b.status) ... possibly another ordering test ....}.tail // take the last one in the ordering
}
rnmwe5a2

rnmwe5a21#

您可以定义scala.math.Ordering[Status]

object StatusOrdering extends Ordering[Status] {
  def compare(x: Status, y: Status): Int =
    (x, y) match {
      // assuming that the ordering is Pending < InProgress < Success < Fail...
      case (_, _) if (x eq y) => 0
      case (Pending, _) => -1
      case (_, Pending) => 1
      case (InProgress, _) => -1
      case (_, InProgress) => 1
      case (Success, _) => -1
      case (_, Success) => 1
      case _ => 0 // (Fail, Fail)
    }

reduce中,您可以

import StatusOrdering.mkOrderingOps

您的Status对象将被<和朋友丰富。
也可以让你的trait扩展Ordered[Status],它在trait中定义了一个规范的顺序:

sealed trait OrderedStatus extends Ordered[OrderedStatus] {
  def compare(that: OrderedStatus): Int =
    (this, that) match {
      case (x, y) if (x eq y) => 0
      case (Qux, _) => -1
      case (_, Qux) => 1
      case (Quux, _) => -1
      case (_, Quux) => 1
      case _ => 0
    }
}

case object Qux extends OrderedStatus
case object Quux extends OrderedStatus
case object Quuux extends OrderedStatus

这样就不必导入mkOrderingOps,但我个人不喜欢在compare方法中向前使用扩展case objects(在每种情况下使用样板compare对象的替代方案更糟糕)。

mzaanser

mzaanser2#

一种方法是在几个Map中定义SystemStatus的优先级,然后通过Ordering.by定义(System, Status)的顺序:

val syMap: Map[System, Int] = Map(A->1, B->2, C->3)
val stMap: Map[Status, Int] = Map(Pending->1, InProgress->2, Success->3, Fail->4)

implicit val ssOrdering: Ordering[(System, Status)] =
  Ordering.by{ case (sy, st) => (syMap.getOrElse(sy, 0), stMap.getOrElse(st, 0)) }

import ssOrdering._

(A, Success) < (C, Pending)
// res1: Boolean = true

(A, Success) < (B, Fail)
// res2: Boolean = true

(C, Pending) < (B, Fail)
// res3: Boolean = false

请注意,在上面的示例代码中,不匹配的System/Status的默认值被设置为0(表示最低优先级)。可以根据需要将它们设置为任何其他值。
要减少(System, Status) s中的Seq

def ssReduce(ss: Seq[(System, Status)])(implicit ssOrd: Ordering[(System, Status)]) : (System, Status) = {
  import ssOrd._
  ss.reduce((acc, t) => if (t < acc) acc else t )  // Or simply `ss.max`
}

ssReduce(Seq((A, Success), (C, Pending), (B, Fail)))
// res4: (System, Status) = (C,Pending)
cwtwac6a

cwtwac6a3#

考虑使用enumatum-cats CatsOrderValueEnum方法定义顺序

import cats.Order
import enumeratum.values._
import cats.instances.int._

sealed abstract class Status(val value: Int) extends IntEnumEntry

object Status extends CatsOrderValueEnum[Int, Status] with IntEnum[Status] {
  case object Pending    extends Status(1)
  case object InProgress extends Status(2)
  case object Success  extends Status(3)
  case object Fail  extends Status(4)

  val values = findValues
}

object AdtOrder extends App {
  import Status._
  println(Order[Status].compare(Pending, Fail))
}

其输出

-1

在哪里

libraryDependencies ++= Seq(
  "com.beachape" %% "enumeratum" % "1.5.13",
  "com.beachape" %% "enumeratum-cats" % "1.5.15"
)
sqserrrh

sqserrrh4#

一旦你给每个case object分配了一个整数,你就可以通过减去它们来编写你自己的Ordering#compare方法:

sealed trait System
case object A extends System
case object B extends System
case object C extends System
case object D extends System
object System {
  implicit val ordering: Ordering[System] = new Ordering[System] {
    def compare(x: System, y: System): Int = {
      def id(value: System): Int = value match {
        case A => 0
        case B => 1
        case C => 2
        case D => 3
      }

      id(x) - id(y)
    }
  }
}

sealed trait Status
case object Pending extends Status
case object InProgress extends Status
case object Success extends Status
case object Fail extends Status

object Status {
  implicit val ordering: Ordering[Status] = new Ordering[Status] {
    def compare(x: Status, y: Status): Int = {
      def id(value: Status): Int = value match {
        case Pending => 0
        case InProgress => 1
        case Success => 2
        case Fail => 3
      }

      id(x) - id(y)
    }
  }
}

然后,您可以轻松地对序列进行排序:

val sorted = Seq((D, Fail), (D,Pending), (A,Fail), (D,InProgress)).sorted
// val sorted = List((A, Fail), (D, Pending), (D, InProgress), (D, Fail))

相关问题