[英]How to write a scala function to accept arguments with two types?
I have two functions: 我有两个功能:
def capPredDouble(rawPred: Double): Double = {
if (label == "1") {
Math.min(1.0, rawPred)
} else {
Math.max(0, rawPred)
}
}
def capPred(rawPred: Float): Float = {
if (label == "1") {
Math.min(1.0f, rawPred)
} else {
Math.max(0, rawPred)
}
}
Is that possible to use polymorphism to make them one function? 可以使用多态性使它们成为一个功能吗?
This appears to work. 这似乎起作用。
def capPred[N](rawPred :N)(implicit ev :Numeric[N]) :N =
if (<some condition>) ev.min(ev.one, rawPred)
else ev.max(ev.zero, rawPred)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.