简化大规模匹配案例-Scala

简化大规模匹配案例-Scala,scala,switch-statement,pattern-matching,refactoring,match,Scala,Switch Statement,Pattern Matching,Refactoring,Match,因此,在其中一个地方,我们有一个巨大的变量匹配case语句 包含近150个不同的大小写语句 看起来很可怕 我想把它分解成更小的函数,我可以把匹配分为10个部分,然后数字case语句减少到15个左右。这很好 现在看起来是这样的 massiveCaseVariable match { case "one" => 1 case "two" => 2 case "three" => 3 case "four" => 4 case "five" => 5

因此,在其中一个地方,我们有一个巨大的
变量匹配case
语句

包含近150个不同的
大小写
语句

看起来很可怕

我想把它分解成更小的函数,我可以把匹配分为10个部分,然后数字
case
语句减少到15个左右。这很好

现在看起来是这样的

massiveCaseVariable match {
  case "one" => 1
  case "two" => 2
  case "three" => 3
  case "four" => 4
  case "five" => 5
  case "six" => 6
}
massiveCaseVariable match {
  case "one" || "two" || "three" => firstCategory(massiveCaseVariable)
  case "four" || "five" || "six" => secondCategory(massiveCaseVariable)
}

def firstCategory(caseVariable: String): Int =
  caseVariable match {
    case "one"   => 1
    case "two"   => 2
    case "three" => 3
  }

def secondCategory(caseVariable: String): Int =
  caseVariable match {
    case "four" => 4
    case "five" => 5
    case "six"  => 6
  }
但我不想这样做

massiveCaseVariable match {
  case "one" => 1
  case "two" => 2
  case "three" => 3
  case "four" => 4
  case "five" => 5
  case "six" => 6
}
massiveCaseVariable match {
  case "one" || "two" || "three" => firstCategory(massiveCaseVariable)
  case "four" || "five" || "six" => secondCategory(massiveCaseVariable)
}

def firstCategory(caseVariable: String): Int =
  caseVariable match {
    case "one"   => 1
    case "two"   => 2
    case "three" => 3
  }

def secondCategory(caseVariable: String): Int =
  caseVariable match {
    case "four" => 4
    case "five" => 5
    case "six"  => 6
  }
太重复了。有没有更好更简洁的方法

我在Scala 2.11上


注:这些示例仅用于说明目的。我绝对不会尝试将字符串与整数匹配

您可以按照以下方式组织它

  val all = firstCategory() ++ secondCategory()

  all("some").apply()

  def firstCategory() : Map[String, () => ()] = {
    Map(
      "first" -> (() => {}),
      "second" -> (() => {})
    )
  }
  def secondCategory(): Map[String, () => ()] = {
    Map(
      "third" -> (() => {
        print("some code")
      }),
      "forth" -> (() => {
        print("some other code")
      })
    )
  }

如果您想简单地组合匹配项,那么您可以注意到这些实际上是部分函数(因为匹配可能失败):

可组合为:

val all = firstCategory orElse secondCategory
all("one")
有趣的是,许多集合都是部分函数,例如
Map
,因此:

val firstCategory = Map[String, Int](
    "one"   -> 1,
    "two"   -> 2,
    "three" -> 3
  )

val secondCategory = Map[String, Int](
    "four" -> 4,
    "five" -> 5,
    "six"  -> 6
  )

val all = firstCategory ++ secondCategory
all("one")

在本例中应以相同的方式工作。

您可以尝试使用提取器对象在
方法中隐藏一些逻辑。如果没有实际的代码,很难做出更好的回答。你能分享你的两个真实案例来描述它们的“重复”性质吗


不要使用模式匹配,或者至少不要单独使用

trait Handler {
def canHandle(variable: String): Boolean
def handle(variable: String): Int
}
class HandlerCategory1 extends Handler {/*...*/}
class HandlerCategory2 extends Handler {/*...*/}
// ...

val variable: String = ???
val handlers = List(new HandlerCategory1(), new HandlerCategory2())

handlers.find(_.canHandle(variable)).map(_.handle(variable))


您可以将模式匹配的条件放入特定的
canHandle()
方法中。

将此标记为正确答案,因为这正好回答了我的问题。事实上,我使用
PartialFunction
orElse
模式重构了
match case