[英]Is it possible to refactor this scala code
我有以下功能:
def firstFunctionMethod(contextTypeName: String): Parser[FunctionCallExpr] = namespace into {
namespaceName =>
function into {
functionName =>
functionExprParameters(contextTypeName) ~ opt(secondFunctionMethod(getFunctionReturnType(functionName).get)) ^^ {
case args ~ subPath => FunctionCallExpr(namespaceName + functionName, args, subPath)
}
}
}
可能的目标类具有10个功能的代码完全相同的问题。 唯一的变化是firstFunctionMethod和secondFunctionMethod始终不同
可以重构吗?
下一个或类似的代码将为您工作。
def firstFunctionMethod(contextTypeName: String): Parser[FunctionCallExpr] = firstFuncTemplate(contextTypeName, secondFunctionMethod) def firstFuncTemplate(contextTypeName: String, secFunc: TypeIn => TypeOut): Parser[FunctionCallExpr] = namespace into { namespaceName => function into { functionName => functionExprParameters(contextTypeName) ~ opt(secFunc(getFunctionReturnType(functionName).get)) ^^ { case args ~ subPath => FunctionCallExpr(namespaceName + functionName, args, subPath) } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.