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這個原理是什麼呢?