Spark学习(函数)

来源:互联网 发布:thinkphp电影网站源码 编辑:程序博客网 时间:2024/06/10 05:11

 
def main(args: Array[String]): Unit = {   //匿名函数 () => Unit 是匿名函数类型 函数体() => println("I'm an anonymous function")   val f1: () => Unit = () => println("I'm an anonymous function")   //匿名函数   val f2= (a:Int)=>{    println(a)   }   var f3 :(Int)=>Unit = (x) => {println(x)} //与f2等效   //f1()   //f2(100)  var theList1 = List(1,2,3,4,5,6,7,8,9)
  //Scala的泛型  var theList2 = GetSort[Int](theList1,5,(x,y)=>x>y)  var theList3 = GetSort[Int](theList1,5,MyCompare)  theList2.foreach(print)  println("===========")  theList3.foreach(print)}def MyCompare(x:Int,y:Int):Boolean={   x>y}//类似于泛型,函数参数def GetSort[T](L1:List[T],MaxL:T, f1:(T,T)=>Boolean): List[T] ={    var theRet : List[T] = List()    for(x <- L1)    {        if(f1(MaxL,x))          {             theRet = x::theRet          }    }    theRet}
后记:不同的语言其实已经非常雷同,原因就是大家都在干类似的事情。
匿名函数和C#的类似,在调用的时候需要注意和C#的lamuda表达式。
0 0