zoukankan      html  css  js  c++  java
  • 大数据笔记(二十五)——Scala函数式编程


    ===================== Scala函数式编程 ========================

    一、Scala中的函数
    (*) 函数是Scala中的头等公民,就和数字一样,可以在变量中存放函数,即:将函数作为变量的值(值函数)

    def myFunc1(name:String):String = "Hello " + name
    println(myFunc1("Tom"))
    
    def myFunc2():String = "Hello World"
    
    //值函数:把函数作为变量的值
    val v1 = myFunc1("Tom")
    val v2 = myFunc2()
    
    //再将v1付给myFunc1
    println(myFunc1(v1))

    运行:

    myFunc1: myFunc1[](val name: String) => String
    Hello Tom
    res0: Unit = ()
    
    myFunc2: myFunc2[]() => String
    
    v1: String = Hello Tom
    v2: String = Hello World
    
    
    Hello Hello Tom
    res1: Unit = ()

    二、匿名函数:没有名字的函数

    //匿名函数:没有名字的函数
    // 完整: def myFunc3(x:Int) = x * 3
    (x:Int) => x*3
    
    //举例:Array(1,2,3) ====> (3,6,9)
    Array(1,2,3).map((x:Int) => x*3)

    运行:

    res2: Int => Int = <function1>
    res3: Array[Int] = Array(3, 6, 9)

    三、高阶函数:带函数参数的函数
    注意:把一个函数作为另外一个函数的参数值

    四、高阶函数示例

    //高阶函数
    import scala.math._
    
    //对数字10进行某种运算
    //f : 就是执行的运算
    def someAction(f:(Double)=>Double) = f(10)
    
    //测试
    //情况1:开平方
    someAction(sqrt)
    someAction(sin)
    
    //另一个例子
    def mytest(x:Int,y:Int):Int = { x*y + 10}
    
    //定义一个高阶函数
    def myFunction(f:(Int,Int)=>Int,x:Int,y:Int)=f(x,y)
    
    //调用
    myFunction(mytest,2,3)

    运行:

    import scala.math._
    
    
    someAction: someAction[](val f: Double => Double) => Double
    
    res0: Double = 3.1622776601683795
    res1: Double = -0.5440211108893698
    
    mytest: mytest[](val x: Int,val y: Int) => Int
    
    myFunction: myFunction[](val f: (Int, Int) => Int,val x: Int,val y: Int) => Int
    
    res2: Int = 16

    数据: 

    val numbers = List(1,2,3,4,5,6,7,8,9,10)

    map: 作用于列表中的每个元素,并且返回一个新的列表

    numbers.map((i:Int) => i*2)

    foreach: 跟map一样,没有返回值

    numbers.foreach((i:Int) => i*2)


    filter: 移除函数函数false的元素
    返回所有能够被2整除的元素

    numbers.filter((i:Int)=> i%2 ==0 )

    zip: 把两个列表合并到一个列表中

    List(1,2,3).zip(List(4,5,6))

    数据: 

    val numbers = List(1,2,3,4,5,6,7,8,9,10)

    partition: 能被2整除的放到一个分区中,不能被整除的放到另一个分区中

    numbers.partition((i:Int) => i%2 ==0)

    find: 第一个匹配条件的元素
    找到第一个能被3整除的元素

    numbers.find((x:Int) => x%3 == 0)
    
    numbers.find(_ % 3 == 0)


    flatten: 把一个嵌套的结构展开

    List(List(1,2,3),List(4,5,6)).flatten


    flatMap: 压平,结合了map和flatten的功能

    var myList = List(List(1,2,3),List(4,5,6))
    myList.flatMap(x=>x.map(_ *2))

    分为两步
    1、List(1,2,3),List(4,5,6) ===> 合并成一个List
    2、再乘以2

    运行以上例子的结果:

    numbers: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
    res0: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
    
    res1: Unit = ()
    
    res2: List[Int] = List(2, 4, 6, 8, 10)
    
    res3: List[(Int, Int)] = List((1,4), (2,5), (3,6))
    
    res4: (List[Int], List[Int]) = (List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9))
    res5: Option[Int] = Some(3)
    
    res6: Option[Int] = Some(3)
    
    res7: List[Int] = List(1, 2, 3, 4, 5, 6)
    
    
    myList: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))
    res8: List[Int] = List(2, 4, 6, 8, 10, 12)
    score: scala.collection.mutable.Map[String,Int] = Map(Mike -> 90)
    res9: Unit = ()
    Map(Mike -> 85)
    res10: Unit = ()

    五、闭包: 就是函数的嵌套
    在一个函数定义中,包含了另一个函数的定义;在内函数中可以访问外函数中的变量

    定义函数:乘法运算

    def myfunc(x:Int,y:Int) = x * y


    采用闭包

    def mulBy(factor:Double)=(x:Double)=> x * factor


    第一个参数:factor 乘法因子,乘以的倍数
    第二个参数:(x:Double)匿名函数,接收一个double数据变量

    测试:
    乘以3倍

    val triple = mulBy(3) //3代表乘法因子

     ---> triple是值函数,接收一个参数(x)

    triple(10) //10就是x,结果30
    triple(6)  //18

    除以2的操作

    val half = mulBy(0.5)
    half(10)  //5


    六、柯里化:Currying
    柯里化函数:把具有多个参数的函数转换成一个函数链,每个节点上的都是一个单一参数的函数
    举例:
    以下两个函数是等价

    def add(x:Int,y:Int)= x + y
    def add(x:Int)(y:Int) = x + y ===> Scala柯里化的语法 
  • 相关阅读:
    先不说 console,其实你连 console.log 都不会
    2019 年终总结 & 2020 年度计划
    将毫秒格式化为天、小时、分钟、秒
    山村老事
    快速更改对象中的字段名
    基于 ECharts 封装甘特图并实现自动滚屏
    JS 将数值取整为10的倍数
    Flutter 徐徐图之(一)—— 从搭建开发环境到 Hello World
    Vue-Cli 3.x 创建的项目中对 import 引入的 CSS 样式启用 autoprefixer
    word——插入目录
  • 原文地址:https://www.cnblogs.com/lingluo2017/p/8678750.html
Copyright © 2011-2022 走看看