Scala currying的例子
发布时间:2020-12-16 18:13:10 所属栏目:安全 来源:网络整理
导读:我是 scala的新手,正在学习 scala功能开发的课程.下面的代码片段解释了currying import math.absobject exercise{ val tolerance = 0.0001 def isCloseEnough(x: Double,y: Double) = abs((x -y)/x)/x tolerance def fixedPoint(f: Double = Double)(firstGu
我是
scala的新手,正在学习
scala功能开发的课程.下面的代码片段解释了currying
import math.abs object exercise{ val tolerance = 0.0001 def isCloseEnough(x: Double,y: Double) = abs((x -y)/x)/x < tolerance def fixedPoint(f: Double => Double)(firstGuess: Double) = { def iterate(guess: Double):Double = { val next = f(guess) if ( isCloseEnough(guess,next)) next else iterate(next) } iterate(firstGuess) } def averageDamp(f: Double => Double)(x: Double) = ( x + f(x))/2 def sqrt(x: Double) = fixedPoint( averageDamp(y => x/y))(1) } 我无法理解代码的以下部分
我知道averageDamp函数有2个参数(一个是函数,另一个是x的值)但是当它从fixedPoint调用时,我们不传递x的值.所以我假设它正在创建一个部分函数,??它被发送回sqrt,其中x的值从sqrt(x:Double)agrument传递.所以我做了以下无法编译的函数
谁可以给我解释一下这个? 解决方法
您需要提供下划线才能在未应用的方法上触发
eta expansion.方法没有值,因此必须将它们转换为函数对象才能分配给变量.当Scala知道应该将未应用的方法解释为函数对象时,会自动触发eta扩展.在其他情况下,您需要使用下划线手动触发扩展.
不确定你的Scala版本是什么,但2.11.7非常清楚.这是一个带缩放功能的简单示例: scala> def my_scaler(sc: Double)(x: Double): Double = sc*x my_scaler: (sc: Double)(x: Double)Double scala> def my_doubler = my_scaler(2d) // no eta exp <console>:13: error: missing argument list for method my_scaler Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing `my_scaler _` or `my_scaler(_)(_)` instead of `my_scaler`. def my_doubler = my_scaler(2d) ^ scala> def my_doubler = my_scaler(2d) _ // manual eta exp my_doubler: Double => Double scala> my_doubler(10d) res1: Double = 20.0 scala> def my_tripler: Double => Double = my_scaler(3d) // auto eta exp my_tripler: Double => Double scala> my_tripler(10d) res2: Double = 30.0 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |