Spark学习(函数)


 
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值