Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 缩短我的代码_Scala - Fatal编程技术网

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-他们手拉手,看到我的答案了吗