Scala 缩短我的代码
我的直觉告诉我下面的代码可以缩短,但我不知道如何缩短。你能帮我吗Scala 缩短我的代码,scala,Scala,我的直觉告诉我下面的代码可以缩短,但我不知道如何缩短。你能帮我吗 def asGraphingFunction[A : Numeric, B : Numeric](f: PartialFunction[A, B]): Double => Double = { val (numericA, numericB) = (implicitly[Numeric[A]], implicitly[Numeric[B]]) (x: Double) => { val xa: A = n
def asGraphingFunction[A : Numeric, B : Numeric](f: PartialFunction[A, B]): Double => Double = {
val (numericA, numericB) = (implicitly[Numeric[A]], implicitly[Numeric[B]])
(x: Double) => {
val xa: A = numericA.fromInt(x.toInt)
if(f.isDefinedAt(xa))
numericB.toDouble(f(xa))
else
0.0
}
}
这个怎么样
import scala.{ PartialFunction => PF }
def asGraphingFunction[A : Numeric, B : Numeric](f: PF[A, B]): Double => Double = {
val pf1: PF[Double,A ] = { case d => numericA.fromInt(d.toInt) }
val pf2: PF[B ,Double] = { case b => numericB.toDouble(b) }
val pf3: PF[Double,Double] = { case _ => 0 }
pf1 andThen f andThen pf2 orElse pf3
}
不是更短,而是更清晰?!有什么意见吗?这个怎么样
import scala.{ PartialFunction => PF }
def asGraphingFunction[A : Numeric, B : Numeric](f: PF[A, B]): Double => Double = {
val pf1: PF[Double,A ] = { case d => numericA.fromInt(d.toInt) }
val pf2: PF[B ,Double] = { case b => numericB.toDouble(b) }
val pf3: PF[Double,Double] = { case _ => 0 }
pf1 andThen f andThen pf2 orElse pf3
}
不是更短,而是更清晰?!有什么意见吗?这里有两个提示:
数值
实例,只需将上下文边界分解为隐式参数就更容易了PartialFunction#lift
将PartialFunction[A,B]
转换为A=>选项[B]
def asGraphingFunction[A, B](f: PartialFunction[A, B])
(implicit numA: Numeric[A], numB: Numeric[B]) =
(x: Double) => f.lift(numA fromInt x.toInt) map (numB.toDouble) getOrElse 0.0
如果使用“正向管道”操作符(来自scalaz或根据定义),则可以使其更加清晰:
def asGraphingFunction[A, B](f: PartialFunction[A, B])
(implicit numA: Numeric[A], numB: Numeric[B]) =
(x: Double) => (numA fromInt x.toInt) |> f.lift map (numB.toDouble) getOrElse 0.0
更新
由于您只转换整数/双精度,实际上根本不需要数字
,因此可以通过java.util.Number
执行所有操作,在过程中删除类型参数:
def asGraphingFunction(f: PartialFunction[Number, _ <: Number]) =
(x: Number) => f.lift(x.intValue) map (_.doubleValue) getOrElse 0.0
def asGraphingFunction(f:PartialFunction[Number,f.lift(x.intValue)map(u.doubleValue)getOrElse 0.0
这里有两个提示:
数值
实例,只需将上下文边界分解为隐式参数就更容易了PartialFunction#lift
将PartialFunction[A,B]
转换为A=>选项[B]
def asGraphingFunction[A, B](f: PartialFunction[A, B])
(implicit numA: Numeric[A], numB: Numeric[B]) =
(x: Double) => f.lift(numA fromInt x.toInt) map (numB.toDouble) getOrElse 0.0
如果使用“正向管道”操作符(来自scalaz或根据定义),则可以使其更加清晰:
def asGraphingFunction[A, B](f: PartialFunction[A, B])
(implicit numA: Numeric[A], numB: Numeric[B]) =
(x: Double) => (numA fromInt x.toInt) |> f.lift map (numB.toDouble) getOrElse 0.0
更新
由于您只转换整数/双精度,实际上根本不需要数字
,因此可以通过java.util.Number
执行所有操作,在过程中删除类型参数:
def asGraphingFunction(f: PartialFunction[Number, _ <: Number]) =
(x: Number) => f.lift(x.intValue) map (_.doubleValue) getOrElse 0.0
def asGraphingFunction(f:PartialFunction[Number,f.lift(x.intValue)map(u.doubleValue)getOrElse 0.0
我不知道Scala,但是你想让代码“更短”有什么特别的原因吗?我觉得这很容易理解,这不是比代码行数更重要吗?@michael-他们齐头并进,看看我的答案我不知道Scala,但是你想让代码“更短”有什么特别的原因吗?对我来说,这似乎相当易读,难道这不比代码行数更重要吗?@michael-他们手拉手,看到我的答案了吗