Scala 无冗余的阵列模式匹配
谁能告诉我如何重写这段代码以避免冗余部分Scala 无冗余的阵列模式匹配,scala,pattern-matching,Scala,Pattern Matching,谁能告诉我如何重写这段代码以避免冗余部分 val lineSplit = line.split(" ") lineSplit match { case Array(cls @ TaggedString(), prop @ TaggedString(), value @ Literal(), ".") => {processProperty(prop);processLiteral(value)} case Array(cls @ Tagge
val lineSplit = line.split(" ")
lineSplit match {
case Array(cls @ TaggedString(), prop @ TaggedString(), value @ Literal(), ".") => {processProperty(prop);processLiteral(value)}
case Array(cls @ TaggedString(), prop @ TaggedString(), value @ LiteralwithSchema(), ".") => {processProperty(prop);processExtendedLiteral(value)}
case Array(cls @ TaggedString(), prop @ TaggedString(), value @ TaggedString(), ".") => {processProperty(prop);processTag(value)}
case _ => throw new IllegalArgumentException("unable to identify line format")
}
正如您可能已经看到的,具有提取特性的零件始终是相同的。
你知道如何充分考虑到这一部分吗
谢谢你的意见
Stefan在您的情况下,我只需将匹配项加倍(尽管也可以编写自定义提取器),并为避免重复,返回一个执行此操作的函数:
def badLine() = throw new IllegalArgumentException("unable to identify line format")
lineSplit match {
case Array(cls @ TaggedString(), prop @ TaggedString(), x, ".") =>
val processValue = x match {
case value: Literal() => () => processLiteral(value)
case value: LiteralwithSchema() ...
...
case _ => badLine()
}
processProperty(prop)
processValue()
case _ => badLine()
}
在您的情况下,我只需将匹配项加倍(尽管也可以编写自定义提取器),并为避免重复,返回一个执行此操作的函数:
def badLine() = throw new IllegalArgumentException("unable to identify line format")
lineSplit match {
case Array(cls @ TaggedString(), prop @ TaggedString(), x, ".") =>
val processValue = x match {
case value: Literal() => () => processLiteral(value)
case value: LiteralwithSchema() ...
...
case _ => badLine()
}
processProperty(prop)
processValue()
case _ => badLine()
}
我决定将处理值的因素考虑到一种对我来说更容易的方法。
现在看看代码,这个问题现在感觉有点尴尬,哈哈。
}
我决定将处理值的因素考虑到一种对我来说更容易的方法。
现在看看代码,这个问题现在感觉有点尴尬,哈哈。
}