匿名函数在函数式编程中经常用到,语法轻量,使用灵活。
不带参数的匿名函数
var noparam = () => {
println("hello world unit")
println("hello unit")
}
var func0 = new Function0[Unit]{
def apply():Unit={
println("hello world unit Function0")
println("Function0")
}
}
带一个参数的匿名函数
var one = (x:Int) => x+1
var func1 = new Function1[Int,Int]{
def apply(x:Int):Int =x+1
}
带多个参数的匿名函数
var two = (x:Int,y:Int) => x+y
var func2 = new Function2[Int,Int,Int]{
def apply(x:Int,y:Int)={
x+y
}
}

本文详细介绍了匿名函数的不同形式及其在函数式编程中的应用。包括不带参数、带一个参数及带多个参数的匿名函数实例。
1229

被折叠的 条评论
为什么被折叠?



