Scala 方法将给定函数应用于给定参数两次

Scala 方法将给定函数应用于给定参数两次,scala,Scala,例如,如果给定Math.sqrt和2.0,它将计算Math.sqrt(Math.sqrt(2.0)) 使用该功能: def applyTwice[A](f: A => A, argument: A) = ??? 然后测试上面的示例如果我正确理解了您的问题,您希望对一个参数应用一个函数两次,并对其进行测试 例如,如果您需要对一个参数应用两次Math.sqrt,您可以实现它,如下代码所示: val sqrt: Double => Double = Math.sqrt def apply

例如,如果给定
Math.sqrt
和2.0,它将计算
Math.sqrt(Math.sqrt(2.0))

使用该功能:

def applyTwice[A](f: A => A, argument: A) = ???

然后测试上面的示例

如果我正确理解了您的问题,您希望对一个参数应用一个函数两次,并对其进行测试

例如,如果您需要对一个参数应用两次
Math.sqrt
,您可以实现它,如下代码所示:

val sqrt: Double => Double = Math.sqrt
def applyTwice[A](f: A => A, d: A) = {
  f(f(d))
}

println(applyTwice[Double](sqrt, 625))

assert(applyTwice[Double](sqrt, 625) == 5.0) // will check if applyTwice return 5.0

这是一个问答网站。你不是在问问题。你的问题是什么?