Scala--高阶函数

高阶函数
(1)将函数赋值给变量
(参数:参数类型。。。)  => 函数体 
	(name:String) => println("hello  " + name)
	把一个匿名函数赋值给一个普通变量
val myFunction = (name:String) => println("hello  " + name)
 print(myFunction("adsf"))
(2)高阶函数
    val arr=Array(1,2,3)
    //map 遍历一个成员,返回一个新的容器
    val t= arr.map((num:Int)=>{ num*2})
    //foreach遍历每个成员,不需要返回一个新的容器
        t.foreach((num:Int)=>print(num))
     println("************")   
     //简写   
    arr.map(_*2).foreach(print(_))
     println("************")  
    /*单词统计*/
    val list=List("haoop","hive","scala","hive","hbase","hadoop")
    print("单词统计:")
   // list.map(word=>(word,1)).foreach(println(_))
   list.map((_,1)).foreach(println(_))

实例01:

  def test(a:Int):Int = {println("this is test");a}
  def hFunction(func: =>Int) {
         val b = func
         println("hello world  " + b)
    }
     hFunction(test(18))
  
实例02:
/*
      参数类型   =>Unit   对传递函数的类型不检查
    即使传递一个带有返回值的函数 def test(a:Int):Int = {println("this is test");a}
    那么func在执行的时候也不会传递返回值 */
     def test2(a:String):Int = {println(a);3}
     def hFunction2(func: (String)=>Unit, name:String) {
     val ret = func(name)
       println("hello world " + ret)
    }
     hFunction2(test2,"bb")
    /* bb
     hello world ()*/
  
     def test3(a:String):String = {println(a);"3"}
     def hFunction3(func: (String)=>String, name:String) {
     val ret = func(name)
       println("hello world " + ret)
    }
     hFunction3(test3,"bb")
   /*  bb
     hello world 3*/
实例03:
//高阶函数式作为返回值
def getGreenFunc(msg:String)=(name:String)=>{println(msg+","+name)}
val names =getGreenFunc("mess")
names("wang")//mess,wang

(3)函数简写
实例01:
def sum(funtion:(Int)=>Int)={funtion(4)}
  println(sum(6*_))
实例02:
  def values(func:(Int)=>Int,low:Int,hight:Int):ArrayBuffer[(Int,Int)]={
     var arr=ArrayBuffer[(Int,Int)]()
    for (tmp <- low to hight){
      arr+=((tmp,func(tmp)))
    }
     arr
  }
  println(values(_*6,-5,5))
  //ArrayBuffer((-5,-30), (-4,-24), (-3,-18), (-2,-12), (-1,-6), (0,0), (1,6), (2,12), (3,18), (4,24), (5,30))

(4)常用高阶函数简写
map和foreach
 //map:对传入的每个元素都进行映射,返回一个处理后的元素
  //foreach对结果遍历
  var arr=Array(1,2,3,4).map(2*_)
      arr.foreach(print(_))//2468
  val arr2=(1 to 9).map("*"*_).foreach(println(_))
 /* *
  **
  ***
  ****
  *****
  ******
  *******
  ********
  **********/
flatMap
//flatMap将list里面的语句拆分成单词
val lst=List("hello world","i have a pen").flatMap(_.split(" "))
println(lst)//List(hello, world, i, have, a, pen)

  filter,过滤
  val arr3=(1 to 20).filter(_%2==0).foreach(println(_))
  /*2
  4
  6
  8
  10
  12
  14
  16
  18
  20*/
  reduceLeft
//reduceLeft :从左侧元素开始,进行reduce操作,即对元素1和元素2进行处理,然后将结果与元素3处理,以此类推
  val arr4= println((1 to 9).reduceLeft(_ * _))//362880 // 类似 1*2*3*4*5*6*7*8*9

  sortWith
  //sortWith对元素进行两两对比,进行排序
  val  arr5=Array(3,2,5,4,10,1).sortWith(_<_).foreach(println(_))
/*  1
  2
  3
  4
  5
  10*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值