scala学习笔记:高阶函数
2015-02-06 08:37
253 查看
scala> def power(y:Double)=(x:Double)=>Math.pow(x,y) warning: there were 1 deprecation warnings; re-run with -deprecation for details power: (y: Double)Double => Double scala> val square=power(2) square: Double => Double = <function1> scala> val squareRoot=power(0.5) squareRoot: Double => Double = <function1> scala> square(10) res0: Double = 100.0 scala> squareRoot(100) res1: Double = 10.0
也可以定义成:
scala> def power = (x:Double)=> ((y:Double) => math.pow(x, y)) power: Double => Double => Double scala> power(4)(3) res8: Double = 64.0
也可以写成:
scala> :paste // Entering paste mode (ctrl-D to finish) def power(x:Double,y:Double) = math.pow(x, y) def square=power(_:Double,2) def squareroot(x:Double)=power(x,0.5) square(3) squareroot(3) // Exiting paste mode, now interpreting. power: (x: Double, y: Double)Double square: Double => Double squareroot: (x: Double)Double res8: Double = 1.7320508075688772
柯里化之后:
def power(x:Double)(y:Double) = math.pow(x, y) def square=power(_:Double)(2) square(3) def squareroot(x:Double)=power(x)(0.5) squareroot(3)
power: (x: Double)(y: Double)Double square: Double => Double squareroot: (x: Double)Double res7: Double = 1.7320508075688772
相关文章推荐
- [Scala]Scala学习笔记八 高阶函数
- Scala2.11.7学习笔记(八)高阶函数
- scala学习笔记:高阶函数
- scala 学习笔记 --高阶函数
- 快学Scala学习笔记及习题解答(12-14高阶函数、集合、模式匹配和样例类)
- scala学习笔记-高阶函数
- Scala 学习笔记(六)------定义高阶函数
- scala学习笔记13 高阶函数
- scala学习笔记--高阶函数
- Scala学习笔记(三)Actor简单例子
- Scala学习笔记(一)简洁之道
- Scala 学习笔记(二)
- Scala学习笔记(九) - 包和引用
- python,ruby,scala学习笔记
- Scala 学习笔记(一)
- Scala 的学习笔记系列(持续更新中)
- Scala学习笔记(2):开发环境的搭建
- Scala学习笔记(3):纯函数式编程的一些思想和技巧
- Scala 学习笔记(二)— Everything is an object
- Scala学习笔记(二)Case的作用