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: