列表上的案例类和模式匹配

发布于 2025-01-03 01:20:31 字数 1272 浏览 1 评论 0原文

我想做的是简化这个表达式

Opt(Opt(ExpList(List(Opt(Opt(Var("optional")))))))

得到类似的东西

Opt(ExpList(List(Opt(Var("optional")))))


匹配表达式会是什么样子,以便简化列表中的所有表达式。

此类任务的最佳实践是什么?

我尝试的代码片段是这样的:

object CaseClassPatternMatching extends App {
  abstract class Expr
  case class Var(name: String) extends Expr
  case class Opt(expr: Expr) extends Expr
  case class ExpList(listExp: List[Expr]) extends Expr

  def joinOpt(feature: Expr): Expr = feature match {
    case Opt(Opt(f)) => joinOpt(Opt(f))    // Opt(Opt("test")) --> Opt("test")
    // case ExpList(list) => ????          // What to do there?
    case _ => feature
  }

  val expr1: Expr = joinOpt(Opt(Opt(Opt(Var("optional")))))
  println(Opt(Var("optional"))) 
  // Output: Opt(Var(optional))  --> That one is OK...

  val expr2: Expr = joinOpt(Opt(Opt(ExpList(List(Opt(Opt(Var("optional"))))))))
  println(expr2)
  // Output: Opt(ExpList(List(Opt(Opt(Var(optional))))))  --> Not OK...
  // How to simplify expressions inside list?
}

[编辑]

对于那些感兴趣的人,类似的主题:

Scala 案例类、模式匹配和可变参数

What I would like to do is simplify this expression

Opt(Opt(ExpList(List(Opt(Opt(Var("optional")))))))

to get something like that

Opt(ExpList(List(Opt(Var("optional")))))


What would match expression look like in order to simplify all expressions inside the list.

What are best-practises for those kind of tasks?

The code snippet I tried is this:

object CaseClassPatternMatching extends App {
  abstract class Expr
  case class Var(name: String) extends Expr
  case class Opt(expr: Expr) extends Expr
  case class ExpList(listExp: List[Expr]) extends Expr

  def joinOpt(feature: Expr): Expr = feature match {
    case Opt(Opt(f)) => joinOpt(Opt(f))    // Opt(Opt("test")) --> Opt("test")
    // case ExpList(list) => ????          // What to do there?
    case _ => feature
  }

  val expr1: Expr = joinOpt(Opt(Opt(Opt(Var("optional")))))
  println(Opt(Var("optional"))) 
  // Output: Opt(Var(optional))  --> That one is OK...

  val expr2: Expr = joinOpt(Opt(Opt(ExpList(List(Opt(Opt(Var("optional"))))))))
  println(expr2)
  // Output: Opt(ExpList(List(Opt(Opt(Var(optional))))))  --> Not OK...
  // How to simplify expressions inside list?
}

[EDIT]

For those of you who are interested, similar topic:

Scala case classes, pattern matching and varargs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2025-01-10 01:20:31

您需要四种情况:

def joinOpt(feature: Expr): Expr = feature match {
                         // remove extra Opt 
                         // (you can use @ to avoid recreating Opt)
  case Opt(opt @ Opt(_)) => joinOpt(opt) 
                         // preserve single Opt
  case Opt(expr)         => Opt(joinOpt(expr)) 
                         // apply function to all elements in inner list
  case ExpList(list)     => ExpList(list map joinOpt) 
  case _ => feature
}

You need four cases:

def joinOpt(feature: Expr): Expr = feature match {
                         // remove extra Opt 
                         // (you can use @ to avoid recreating Opt)
  case Opt(opt @ Opt(_)) => joinOpt(opt) 
                         // preserve single Opt
  case Opt(expr)         => Opt(joinOpt(expr)) 
                         // apply function to all elements in inner list
  case ExpList(list)     => ExpList(list map joinOpt) 
  case _ => feature
}
执笏见 2025-01-10 01:20:31

好吧,我会这样写 joinOpt

def joinOpt(feature: Expr): Expr = feature match {
  case Opt(Opt(f))   => joinOpt(Opt(f))    // Opt(Opt("test")) --> Opt("test")
  case ExpList(list) => ExpList(list map joinOpt)
  case other         => other
}

Well, I'd write joinOpt like this:

def joinOpt(feature: Expr): Expr = feature match {
  case Opt(Opt(f))   => joinOpt(Opt(f))    // Opt(Opt("test")) --> Opt("test")
  case ExpList(list) => ExpList(list map joinOpt)
  case other         => other
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文