尚硅谷大数据技术Scala教程-笔记02【函数式编程】

视频地址:尚硅谷大数据技术之Scala入门到精通教程(小白快速上手scala)_哔哩哔哩_bilibili

  1. 尚硅谷大数据技术Scala教程-笔记01【Scala课程简介、Scala入门、变量和数据类型、运算符、流程控制】
  2. 尚硅谷大数据技术Scala教程-笔记02【函数式编程】
  3. 尚硅谷大数据技术Scala教程-笔记03【面向对象】
  4. 尚硅谷大数据技术Scala教程-笔记04【集合】
  5. 尚硅谷大数据技术Scala教程-笔记05【模式匹配、异常】

目录

第05章-Scala课程简介

P048【048_尚硅谷_Scala_函数式编程(一)_函数式编程思想】15:11

P049【049_尚硅谷_Scala_函数式编程(二)_函数基础(一)_函数和方法】13:12

P050【050_尚硅谷_Scala_函数式编程(二)_函数基础(二)_函数定义】10:03

P051【051_尚硅谷_Scala_函数式编程(二)_函数基础(三)_函数参数特殊用法】12:39

P052【052_尚硅谷_Scala_函数式编程(二)_函数基础(四)_函数至简原则】18:56

P053【053_尚硅谷_Scala_函数式编程(三)_函数高级(一)_匿名函数(一)_概念及简化规则】16:09

P054【054_尚硅谷_Scala_函数式编程(三)_函数高级(一)_匿名函数(二)_示例】11:21

P055【055_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(一)_函数作为值传递】11:41

P056【056_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(二)_函数作为参数传递】04:34

P057【057_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(三)_函数作为返回值】05:55

P058【058_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(四)_应用案例】12:58

P059【059_尚硅谷_Scala_函数式编程(三)_函数高级(三)_扩展练习(一)_匿名函数】04:48

P060【060_尚硅谷_Scala_函数式编程(三)_函数高级(三)_扩展练习(二)_函数作为返回值】13:50

P061【061_尚硅谷_Scala_函数式编程(三)_函数高级(四)_闭包(一)_概念和原理】10:52

P062【062_尚硅谷_Scala_函数式编程(三)_函数高级(四)_闭包(二)_具体应用】17:54

P063【063_尚硅谷_Scala_函数式编程(三)_函数高级(四)_柯里化】06:16

P064【064_尚硅谷_Scala_函数式编程(三)_函数高级(五)_递归(一)_概念和实现】10:19

P065【065_尚硅谷_Scala_函数式编程(三)_函数高级(五)_递归(二)_尾递归优化】17:50

P066【066_尚硅谷_Scala_函数式编程(三)_函数高级(六)_控制抽象(一)_传值参数】04:34

P067【067_尚硅谷_Scala_函数式编程(三)_函数高级(六)_控制抽象(二)_传名参数】08:52

P068【068_尚硅谷_Scala_函数式编程(三)_函数高级(六)_控制抽象(三)_自定义While循环】16:34

P069【069_尚硅谷_Scala_函数式编程(三)_函数高级(七)_惰性加载】06:33


第05章-Scala课程简介

P048【048_尚硅谷_Scala_函数式编程(一)_函数式编程思想】15:11

  • 1)面向对象编程
  • 2)函数式编程
  • 3)在Scala中函数式编程和面向对象编程完美融合在一起了。

P049【049_尚硅谷_Scala_函数式编程(二)_函数基础(一)_函数和方法】13:12

package chapter05

object Test01_FunctionAndMethod {
  def main(args: Array[String]): Unit = {
    // 定义函数
    def sayHi(name: String): Unit = {
      println("hi, " + name)
    }

    // 调用函数
    sayHi("alice")

    // 调用对象方法
    Test01_FunctionAndMethod.sayHi("bob")

    // 获取方法返回值
    val result = Test01_FunctionAndMethod.sayHello("cary")
    println(result)
  }

  // 定义对象的方法
  def sayHi(name: String): Unit = {
    println("Hi, " + name)
  }

  def sayHello(name: String): String = {
    println("Hello, " + name)
    return "Hello"
  }
}

P050【050_尚硅谷_Scala_函数式编程(二)_函数基础(二)_函数定义】10:03

package chapter05

object Test02_FunctionDefine {
  def main(args: Array[String]): Unit = {
    //    (1)函数1:无参,无返回值
    def f1(): Unit = {
      println("1. 无参,无返回值")
    }

    f1()
    println(f1())

    println("=========================")

    //    (2)函数2:无参,有返回值
    def f2(): Int = {
      println("2. 无参,有返回值")
      return 12
    }

    println(f2())

    println("=========================")

    //    (3)函数3:有参,无返回值
    def f3(name: String): Unit = {
      println("3:有参,无返回值 " + name)
    }

    println(f3("alice"))

    println("=========================")

    //    (4)函数4:有参,有返回值
    def f4(name: String): String = {
      println("4:有参,有返回值 " + name)
      return "hi, " + name
    }

    println(f4("alice"))

    println("=========================")

    //    (5)函数5:多参,无返回值
    def f5(name1: String, name2: String): Unit = {
      println("5:多参,无返回值")
      println(s"${name1}和${name2}都是我的好朋友")
    }

    f5("alice", "bob")

    println("=========================")

    //    (6)函数6:多参,有返回值
    def f6(a: Int, b: Int): Int = {
      println("6:多参,有返回值")
      return a + b
    }

    println(f6(12, 37))
  }
}

P051【051_尚硅谷_Scala_函数式编程(二)_函数基础(三)_函数参数特殊用法】12:39

package chapter05

object Test03_FunctionParameter {
  def main(args: Array[String]): Unit = {
    //    (1)可变参数
    def f1(str: String*): Unit = {
      println(str)
    }

    f1("alice")
    f1("aaa", "bbb", "ccc")

    //    (2)如果参数列表中存在多个参数,那么可变参数一般放置在最后
    def f2(str1: String, str2: String*): Unit = {
      println("str1: " + str1 + ",str2: " + str2)
    }

    f2("alice")
    f2("aaa", "bbb", "ccc")

    //    (3)参数默认值,一般将有默认值的参数放置在参数列表的后面
    def f3(name: String = "atguigu"): Unit = {
      println("My school is " + name)
    }

    f3("school")
    f3()

    //    (4)带名参数
    def f4(name: String = "atguigu", age: Int): Unit = {
      println(s"${age}岁的${name}在尚硅谷学习")
    }

    f4("alice", 20)
    f4(age = 23, name = "bob")
    f4(age = 21)
  }
}

P052【052_尚硅谷_Scala_函数式编程(二)_函数基础(四)_函数至简原则】18:56

package chapter05

// 函数至简原则
object Test04_Simplify {
  def main(args: Array[String]): Unit = {

    def f0(name: String): String = {
      return name
    }

    println(f0("atguigu"))

    println("==========================")

    //    (1)return可以省略,Scala会使用函数体的最后一行代码作为返回值
    def f1(name: String): String = {
      name
    }

    println(f1("atguigu"))

    println("==========================")

    //    (2)如果函数体只有一行代码,可以省略花括号
    def f2(name: String): String = name

    println(f2("atguigu"))

    println("==========================")

    //    (3)返回值类型如果能够推断出来,那么可以省略(:和返回值类型一起省略)
    def f3(name: String) = name

    println(f3("atguigu"))

    println("==========================")

    //    (4)如果有return,则不能省略返回值类型,必须指定
    //    def f4(name: String) = {
    //      return name
    //    }
    //
    //    println(f4("atguigu"))

    println("==========================")

    //    (5)如果函数明确声明unit,那么即使函数体中使用return关键字也不起作用
    def f5(name: String): Unit = {
      return name
    }

    println(f5("atguigu"))

    println("==========================")

    //    (6)Scala如果期望是无返回值类型,可以省略等号
    def f6(name: String) {
      println(name)
    }

    println(f6("atguigu"))

    println("==========================")

    //    (7)如果函数无参,但是声明了参数列表,那么调用时,小括号,可加可不加
    def f7(): Unit = {
      println("atguigu")
    }

    f7()
    f7

    println("==========================")

    //    (8)如果函数没有参数列表,那么小括号可以省略,调用时小括号必须省略
    def f8: Unit = {
      println("atguigu")
    }

    //    f8()
    f8

    println("==========================")

    //    (9)如果不关心名称,只关心逻辑处理,那么函数名(def)可以省略
    def f9(name: String): Unit = {
      println(name)
    }

    // 匿名函数,lambda表达式
    (name: String) => {
      println(name)
    }
    println("==========================")
  }
}

P053【053_尚硅谷_Scala_函数式编程(三)_函数高级(一)_匿名函数(一)_概念及简化规则】16:09

package chapter05

object Test05_Lambda {
  def main(args: Array[String]): Unit = {
    val fun = (name: String) => {
      println(name)
    }
    fun("atguigu")

    println("========================")

    // 定义一个函数,以函数作为参数输入
    def f(func: String => Unit): Unit = {
      func("atguigu")
    }

    f(fun)
    f((name: String) => {
      println(name)
    })

    println("========================")

    // 匿名函数的简化原则
    //    (1)参数的类型可以省略,会根据形参进行自动的推导
    f((name) => {
      println(name)
    })

    //    (2)类型省略之后,发现只有一个参数,则圆括号可以省略;其他情况:没有参数和参数超过1的永远不能省略圆括号。
    f(name => {
      println(name)
    })

    //    (3)匿名函数如果只有一行,则大括号也可以省略
    f(name => println(name))

    //    (4)如果参数只出现一次,则参数省略且后面参数可以用_代替
    f(println(_))

    //     (5) 如果可以推断出,当前传入的println是一个函数体,而不是调用语句,可以直接省略下划线
    f(println)

    println("=========================")
  }
}

P054【054_尚硅谷_Scala_函数式编程(三)_函数高级(一)_匿名函数(二)_示例】11:21

package chapter05

object Test05_Lambda {
  def main(args: Array[String]): Unit = {
    // 实际示例,定义一个”二元运算“函数,只操作1和2两个数,但是具体运算通过参数传入
    def dualFunctionOneAndTwo(fun: (Int, Int) => Int): Int = {
      fun(1, 2)
    }

    val add = (a: Int, b: Int) => a + b
    val minus = (a: Int, b: Int) => a - b

    println(dualFunctionOneAndTwo(add))
    println(dualFunctionOneAndTwo(minus))

    // 匿名函数简化
    println(dualFunctionOneAndTwo((a: Int, b: Int) => a + b))
    println(dualFunctionOneAndTwo((a: Int, b: Int) => a - b))

    println(dualFunctionOneAndTwo((a, b) => a + b))
    println(dualFunctionOneAndTwo(_ + _))
    println(dualFunctionOneAndTwo(_ - _))

    println(dualFunctionOneAndTwo((a, b) => b - a))
    println(dualFunctionOneAndTwo(-_ + _))
  }
}

P055【055_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(一)_函数作为值传递】11:41

package chapter05

object Test06_HighOrderFunction {
  def main(args: Array[String]): Unit = {
    def f(n: Int): Int = {
      println("f调用")
      n + 1
    }

    def fun(): Int = {
      println("fun调用")
      1
    }

    val result: Int = f(123)
    println(result)

    // 1. 函数作为值进行传递
    val f1: Int => Int = f
    val f2 = f _

    println(f1)
    println(f1(12))
    println(f2)
    println(f2(35))

    val f3: () => Int = fun
    val f4 = fun _
    println(f3)
    println(f4)
  }
}

P056【056_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(二)_函数作为参数传递】04:34

package chapter05

object Test06_HighOrderFunction {
  def main(args: Array[String]): Unit = {
    // 2. 函数作为参数进行传递
    // 定义二元计算函数
    def dualEval(op: (Int, Int) => Int, a: Int, b: Int): Int = {
      op(a, b)
    }

    def add(a: Int, b: Int): Int = {
      a + b
    }

    println(dualEval(add, 12, 35))
    println(dualEval((a, b) => a + b, 12, 35))
    println(dualEval(_ + _, 12, 35))

    // 3. 函数作为函数的返回值返回
    def f5(): Int => Unit = {
      def f6(a: Int): Unit = {
        println("f6调用 " + a)
      }

      f6 // 将函数直接返回
    }

    //    val f6 = f5()
    //    println(f6)
    //    println(f6(25))

    println(f5()(25))
  }
}

P057【057_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(三)_函数作为返回值】05:55

package chapter05

object Test06_HighOrderFunction {
  def main(args: Array[String]): Unit = {
    // 3. 函数作为函数的返回值返回
    def f5(): Int => Unit = {
      def f6(a: Int): Unit = {
        println("f6调用 " + a)
      }

      f6 // 将函数直接返回
    }

    //    val f6 = f5()
    //    println(f6)
    //    println(f6(25))

    println(f5()(25))
  }
}

P058【058_尚硅谷_Scala_函数式编程(三)_函数高级(二)_高阶函数(四)_应用案例】12:58

package chapter05

object Test07_Practice_CollectionOperation {
  def main(args: Array[String]): Unit = {
    val arr: Array[Int] = Array(12, 45, 75, 98)

    // 对数组进行处理,将操作抽象出来,处理完毕之后的结果返回一个新的数组
    def arrayOperation(array: Array[Int], op: Int => Int): Array[Int] = {
      for (elem <- array) yield op(elem)
    }

    // 定义一个加一操作
    def addOne(elem: Int): Int = {
      elem + 1
    }

    // 调用函数
    val newArray: Array[Int] = arrayOperation(arr, addOne)

    println(newArray.mkString(","))

    // 传入匿名函数,实现元素翻倍
    val newArray2 = arrayOperation(arr, _ * 2)
    println(newArray2.mkString(", "))
  }
}

P059【059_尚硅谷_Scala_函数式编程(三)_函数高级(三)_扩展练习(一)_匿名函数】04:48

package chapter05

object Test08_Practice {
  def main(args: Array[String]): Unit = {
    // 1. 练习1
    val fun = (i: Int, s: String, c: Char) => {
      if (i == 0 && s == "" && c == '0') false else true
    }

    println(fun(0, "", '0'))//false
    println(fun(0, "", '1'))//true
    println(fun(23, "", '0'))//true
    println(fun(0, "hello", '0'))//true
  }
}

P060【060_尚硅谷_Scala_函数式编程(三)_函数高级(三)_扩展练习(二)_函数作为返回值】13:50

package chapter05

object Test08_Practice {
  def main(args: Array[String]): Unit = {
    // 2. 练习2
    def func(i: Int): String => (Char => Boolean) = {
      def f1(s: String): Char => Boolean = {
        def f2(c: Char): Boolean = {
          if (i == 0 && s == "" && c == '0') false else true
        }
        f2
      }
      f1
    }

    println(func(0)("")('0'))//false
    println(func(0)("")('1'))//true
    println(func(23)("")('0'))//true
    println(func(0)("hello")('0'))//true

    // 匿名函数简写
    def func1(i: Int): String => (Char => Boolean) = {
      s => c => if (i == 0 && s == "" && c == '0') false else true
    }

    println(func1(0)("")('0'))//false
    println(func1(0)("")('1'))//true
    println(func1(23)("")('0'))//true
    println(func1(0)("hello")('0'))//true

    // 柯里化
    def func2(i: Int)(s: String)(c: Char): Boolean = {
      if (i == 0 && s == "" && c == '0') false else true
    }

    println(func2(0)("")('0'))//false
    println(func2(0)("")('1'))//true
    println(func2(23)("")('0'))//true
    println(func2(0)("hello")('0'))//true
  }
}

P061【061_尚硅谷_Scala_函数式编程(三)_函数高级(四)_闭包(一)_概念和原理】10:52

P062【062_尚硅谷_Scala_函数式编程(三)_函数高级(四)_闭包(二)_具体应用】17:54

package chapter05

object Test09_ClosureAndCurrying {
  def main(args: Array[String]): Unit = {
    def add(a: Int, b: Int): Int = {
      a + b
    }

    // 1. 考虑固定一个加数的场景
    def addByFour(b: Int): Int = {
      4 + b
    }

    // 2. 扩展固定加数改变的情况
    def addByFive(b: Int): Int = {
      5 + b
    }

    // 3. 将固定加数作为另一个参数传入,但是将其作为”第一层参数“传入
    def addByFour1(): Int => Int = {
      val a = 4
      def addB(b: Int): Int = {
        a + b
      }

      addB
    }

    def addByA(a: Int): Int => Int = {
      def addB(b: Int): Int = {
        a + b
      }
      addB
    }

    println(addByA(35)(24))

    val addByFour2 = addByA(4)
    val addByFive2 = addByA(5)

    println(addByFour2(13))
    println(addByFive2(25))

    // 4. lambda表达式简写
    def addByA1(a: Int): Int => Int = {
      (b: Int) => {
        a + b
      }
    }

    def addByA2(a: Int): Int => Int = {
      b => a + b
    }

    def addByA3(a: Int): Int => Int = a + _

    val addByFour3 = addByA3(4)
    val addByFive3 = addByA3(5)

    println(addByFour3(13))
    println(addByFive3(25))
  }
}

P063【063_尚硅谷_Scala_函数式编程(三)_函数高级(四)_柯里化】06:16

package chapter05

object Test09_ClosureAndCurrying {
  def main(args: Array[String]): Unit = {
    // 5. 柯里化
    def addCurrying(a: Int)(b: Int): Int = {
      a + b
    }
    println(addCurrying(35)(24))
  }
}

P064【064_尚硅谷_Scala_函数式编程(三)_函数高级(五)_递归(一)_概念和实现】10:19

public class TestRecursion {
    public static void main(String[] args) {
        // 计算阶乘
        System.out.println(factorial(5));
        System.out.println(fact(5));
    }

    // 1. 循环实现
    public static int factorial(int n) {
        int result = 1;
        for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }

    // 2. 递归实现
    public static int fact(int n) {
        // 基准情形,0! = 1
        if (n == 0) return 1;
        return fact(n - 1) * n;
    }
}

P065【065_尚硅谷_Scala_函数式编程(三)_函数高级(五)_递归(二)_尾递归优化】17:50

package chapter05

import scala.annotation.tailrec

object Test10_Recursion {
  def main(args: Array[String]): Unit = {
    println(fact(5))
    println(tailFact(5))
  }

  //递归实现计算阶乘
  def fact(n: Int): Int = {
    if (n == 0) return 1
    fact(n - 1) * n
  }

  //尾递归实现
  def tailFact(n: Int): Int = {
    @tailrec
    def loop(n: Int, currRes: Int): Int = {
      if (n == 0) return currRes
      loop(n - 1, currRes * n)
    }

    loop(n, 1)
  }
}

P066【066_尚硅谷_Scala_函数式编程(三)_函数高级(六)_控制抽象(一)_传值参数】04:34

package chapter05

object Test11_ControlAbstraction {
  def main(args: Array[String]): Unit = {
    // 1. 传值参数
    def f0(a: Int): Unit = {
      println("a: " + a)
      println("a: " + a)
    }

    f0(23)

    def f1(): Int = {
      println("f1调用")
      12
    }

    f0(f1())
  }
}

P067【067_尚硅谷_Scala_函数式编程(三)_函数高级(六)_控制抽象(二)_传名参数】08:52

package chapter05

object Test11_ControlAbstraction {
  def main(args: Array[String]): Unit = {
    // 2. 传名参数,传递的不再是具体的值,而是代码块
    def f2(a: => Int): Unit = {
      println("a: " + a)
      println("a: " + a)
    }

    f2(23)
    f2(f1())

    f2({
      println("这是一个代码块")
      29
    })
  }
}

P068【068_尚硅谷_Scala_函数式编程(三)_函数高级(六)_控制抽象(三)_自定义While循环】16:34

package chapter05

object Test12_MyWhile {
  def main(args: Array[String]): Unit = {
    var n = 10

    // 1. 常规的while循环
    while (n >= 1) {
      println(n)
      n -= 1
    }

    // 2. 用闭包实现一个函数,将代码块作为参数传入,递归调用
    def myWhile(condition: => Boolean): (=> Unit) => Unit = {
      // 内层函数需要递归调用,参数就是循环体
      def doLoop(op: => Unit): Unit = {
        if (condition) {
          op
          myWhile(condition)(op)
        }
      }
      doLoop _
    }

    println("=================")
    n = 10
    myWhile(n >= 1) {
      println(n)
      n -= 1
    }

    // 3. 用匿名函数实现
    def myWhile2(condition: => Boolean): (=> Unit) => Unit = {
      // 内层函数需要递归调用,参数就是循环体
      op => {
        if (condition) {
          op
          myWhile2(condition)(op)
        }
      }
    }

    println("=================")
    n = 10
    myWhile2(n >= 1) {
      println(n)
      n -= 1
    }

    // 3. 用柯里化实现
    def myWhile3(condition: => Boolean)(op: => Unit): Unit = {
      if (condition) {
        op
        myWhile3(condition)(op)
      }
    }

    println("=================")
    n = 10
    myWhile3(n >= 1) {
      println(n)
      n -= 1
    }
  }
}

P069【069_尚硅谷_Scala_函数式编程(三)_函数高级(七)_惰性加载】06:33

package chapter05

object Test13_Lazy {
  def main(args: Array[String]): Unit = {
    lazy val result: Int = sum(13, 47)

    println("1. 函数调用")
    println("2. result = " + result)
    println("4. result = " + result)
  }

  def sum(a: Int, b: Int): Int = {
    println("3. sum调用")
    a + b
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_44949135/article/details/129956420