def main(args:Array[String])
{
val v = (x:Int,y:Int)=> {
if (x >10)
x+y
else
x*y
}
println(v(3,4))
println(anonyMouseFun(3)(5))
println(afun(4)(5))
def afun(x:Int) = (a:Int) => v(2,3)*a*x
println(bfun(4)(5))
def bfun(x:Int) = (b:Int) => {
var fr = v(2,3)
var fl=fr*b*x
println(s"fl=$fl")
(fl*2 + 10) / 50
}
}
def anonyMouseFun(x:Int) = (a:Int)=>x*aapp
打印结果:函数
12
15
120
fl=120
5学习
以上实验主要学习返回匿名函数做为函数的返回值,那如何练习给一个函数传递函数参数呢?看下面代码:this
def main(args:Array[String])
{
val v = (x:Int,y:Int)=> {
if (x >10)
x+y
else
x*y
}
println(v(3,4))
println(anonyMouseFun(3)(5))
println(afun(4)(5))
def afun(x:Int) = (a:Int) => v(2,3)*a*x
println(bfun(4)(5))
def bfun(x:Int) = (b:Int) => {
var fr = v(2,3)
var fl=fr*b*x
println(s"fl=$fl")
(fl*2 + 10) / 50
}
println(anonyMouseFun3(itoi))
def itoi(x:Int) = x+5
def anonyMouseFun3(f:(Int)=>Int):Int = {
val av = f(v(3,4))
av*5+10
}
println(anonyMouseFun2(itoi))
}
def anonyMouseFun(x:Int) = (a:Int)=>x*a
def anonyMouseFun2(f:(Int)=>Int):Int = {
val av = f(3)
av*5+10
}spa
打印结果:scala
12
15
120
fl=120
5
95
50code
另外,还发现一个现象:ci
import scala.math._it
val num=3.14
val fun=ceilio
出现错误提示:missing argument list for method ceil in package math Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing ceil _
or ceil(_)
instead of ceil
.
若是换成 val fun=ceil _ 则编译就能成功,打印结果为 4.0这个原理是什么呢?