Scala重构重复代码
Scala refactoring duplicate code
我是 Scala 的新手,正在尝试弄清楚如何重构以下代码以避免重复函数。如您所见,它们非常相似,只需要重构为通用代码即可。
protected lazy val shardNamesSpec: Parser[Seq[String]] = {
(ident ^^ {case d => Seq(d)}) | ("(" ~> repsep(ident, ",") <~ ")") ^^ {
case i => i
}
}
protected lazy val shardTypesSpec: Parser[Seq[DataType]] = {
(dataType ^^ {case d => Seq(d)}) | ("(" ~> repsep(dataType, ",") <~ ")") ^^ {
case dt => dt
}
}
我想你需要以下内容:
def parseExpr[T](value: Parser[T]): Parser[Seq[T]] = {
(value ^^ {case d => Seq(d)}) | ("(" ~> repsep(value, ",") <~ ")") ^^ {
case i => i
}
}
protected lazy val shardNamesSpec: Parser[Seq[String]] = parseExpr(ident)
protected lazy val shardTypesSpec: Parser[Seq[DataType]] = parseExpr(dataType)
我是 Scala 的新手,正在尝试弄清楚如何重构以下代码以避免重复函数。如您所见,它们非常相似,只需要重构为通用代码即可。
protected lazy val shardNamesSpec: Parser[Seq[String]] = {
(ident ^^ {case d => Seq(d)}) | ("(" ~> repsep(ident, ",") <~ ")") ^^ {
case i => i
}
}
protected lazy val shardTypesSpec: Parser[Seq[DataType]] = {
(dataType ^^ {case d => Seq(d)}) | ("(" ~> repsep(dataType, ",") <~ ")") ^^ {
case dt => dt
}
}
我想你需要以下内容:
def parseExpr[T](value: Parser[T]): Parser[Seq[T]] = {
(value ^^ {case d => Seq(d)}) | ("(" ~> repsep(value, ",") <~ ")") ^^ {
case i => i
}
}
protected lazy val shardNamesSpec: Parser[Seq[String]] = parseExpr(ident)
protected lazy val shardTypesSpec: Parser[Seq[DataType]] = parseExpr(dataType)