白话scala系列五 匿名函数分析

来源:互联网 发布:linux send 返回值 编辑:程序博客网 时间:2024/05/18 02:41

匿名函数在函数式编程中经常用到,语法轻量,使用灵活。

不带参数的匿名函数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+1var func1 = new Function1[Int,Int]{    def apply(x:Int):Int =x+1}带多个参数的匿名函数var two = (x:Int,y:Int) => x+yvar func2 = new Function2[Int,Int,Int]{    def apply(x:Int,y:Int)={        x+y    }}
0 0