• 第六章 函数式编程


    一. 函数

    1. 函数是组织好的、可重复使用的、用于执行指定任务的代码块。Go语言中支持函数、匿名函数和闭包,并且函数在Go语言中属于“一等公民”。

    2. 函数的定义

    Go语言中定义函数使用func关键字

    func 函数名(参数)(返回值){
        函数体
    }

    定义规则:

    函数名:由字母、数字、下划线组成。但函数名的第一个字母不能是数字。在同一个包内,函数名也称不能重名(包的概念详见后文)。
    参数:参数由参数变量和参数变量的类型组成,多个参数之间使用,分隔。
    返回值:返回值由返回值变量和其变量类型组成,也可以只写返回值的类型,多个返回值必须用()包裹,并用,分隔。
    函数体:实现指定功能的代码块。
    

    3. 可变参数

    可变参数是指函数的参数数量不固定。Go语言中的可变参数通过在参数名后加...来标识。

    注意:可变参数通常要作为函数的最后一个参数。例:

    func intSum2(x ...int) int {
        fmt.Println(x) //x是一个切片
        sum := 0
        for _, v := range x {
            sum = sum + v
        }
        return sum
    }

    调用

    ret1 := intSum2()
    ret2 := intSum2(10)
    ret3 := intSum2(10, 20)
    ret4 := intSum2(10, 20, 30)
    fmt.Println(ret1, ret2, ret3, ret4) //0 10 30 60

    二. 函数类型与变量

    我们可以使用type关键字来定义一个函数类型,具体格式如下:

    type calculate func(int, int) int

    上面语句定义了一个calculation类型,它是一种函数类型,这种函数接收两个int类型的参数并且返回一个int类型的返回值。

    简单来说,凡是满足这个条件的函数都是calculation类型的函数,例如下面的add和sub是calculation类型。

    func add(x int, y int) int {
        return x + y
    }
    
    func sub(x int, y int) int {
        return x - y
    }

    add和sub都能赋值给calculation类型的变量。

    func main() {
        var c calculate
        c = add
        fmt.Printf("%T 
    ", c)  // main.calculate 
        fmt.Println(c(1,2 ))  //3
        c = sub
        fmt.Println(c(5,4)) //1
    }

    三. 高阶函数

    高阶函数分为函数作为参数和函数作为返回值

    1. 函数作为参数

    func calc(x int, y int, op func(int, int) int) int {
        return op(x, y)
    }

    调用

    func main() {
        cal  := calc(1, 2, add)  
        fmt.Println(cal)  // 3
    }

    2. 函数作为返回值

    func do(x string) (func(int, int) int, error){
        switch x {
        case "add":
            return add, nil
        case "sub":
            return sub, nil
        default:
            panic("error")
        }
    }
    func main() {
        f, e := do("add")
        if e == nil {
            r := f(2, 3)
            fmt.Printf("%d", r)  //5
        }
    }

    四. 匿名函数和闭包

    1. 匿名函数

    函数可以作为返回值,但在Go语言中函数内部不能再像之前那样定义函数了,只能定义匿名函数。匿名函数就是没有函数名的函数,

    匿名函数的定义格式如下:

    func(参数)(返回值){
        函数体
    }

    匿名函数因为没有函数名,所以没办法像普通函数那样调用,所以匿名函数需要保存到某个变量或者作为立即执行函数:

    func main() {
        // 将匿名函数保存到变量
        a := func(x, y int) int {
            return x + y
        }
        a(2,4)  // 通过变量调用匿名函数
    
    
        //自执行函数:匿名函数定义完加()直接执行
        func(x, y int) int{
            return x + y
        }(3, 5)
    }

    匿名函数多用于实现回调函数和闭包。

    2. 闭包

    闭包指的是一个函数和与其相关的引用环境组合而成的实体。简单来说,闭包=函数+引用环境。 首先我们来看一个例子:

    func adder() func(int) int {
        x := 5
        return func(y int) int {
            x += y
            return x
        }
    }
    
    func main() {
        f := adder()
        fmt.Println(f(6))  //11
        fmt.Println(f(7))  // 18
    }

    变量f是一个函数并且它引用了其外部作用域中的x变量,此时f就是一个闭包。 在f的生命周期内,变量x也一直有效。 

    x为什么有效呢? x始终作为闭包的返回值, 返回给了f.

     例2: 带有多个返回值函数的

    func calcu(base int) (func(int) int, func(int) int) {
        add := func( y int) int {
            base += y
            return base
    
        }
    
        sub := func(y int) int {
            base -= y
            return base
        }
        return add, sub
    }
    
    func main() {
        f1, f2 := calcu(10)
        fmt.Println(f1(10), f2(20))  // 20 0
    }

    五. 函数式编程

    1. 函数是一等公民: 参数, 变量, 返回值都可以是函数
    2. 高阶函数: 因为参数变量,返回值都可以是函数, 所以是一种高阶函数
    3. 函数->闭包

    我们来看一个例子

    package main
    
    import "fmt"
    
    // 定义一个累加器
    func adder() func(int) int {
        sum := 0
        return func(i int) int {
            sum += i
            return sum
        }
    }
    func main() {
        f := adder()
    
        for i := 0; i < 10 ; i++  {
            fmt.Printf("0 + 1 ....+ %d = %d 
    ", i, f(i))
        }
    }

    返回结果:

    0 + 1 +....+ 0 = 0 
    0 + 1 +....+ 1 = 1 
    0 + 1 +....+ 2 = 3 
    0 + 1 +....+ 3 = 6 
    0 + 1 +....+ 4 = 10 
    0 + 1 +....+ 5 = 15 
    0 + 1 +....+ 6 = 21 
    0 + 1 +....+ 7 = 28 
    0 + 1 +....+ 8 = 36 
    0 + 1 +....+ 9 = 45 

    adder函数里有一个变量sum, 这个函数保存了sum的值. 因此, 每次累加的时候, 都是在上一次的基础上加. 

      第一次累加结果是0 , 第二次是1, 第三次在第二次的sum上累加,结果是2 ......

    1. 闭包

    首先, 函数体里面有局部变量, 参数可以看做局部变量.

    // 定义一个累加器
    func adder() func(int) int {
        sum := 0
        return func(v int) int {
            sum += v
            return sum
        }
    }

    函数体还引用了外部的变量, 这个外部变量对于函数体来说就是自由变量

    上面红色代码部分就是返回函数的函数体. 他有一个局部变量v, 他里面还有一个sum, sum不是函数体里面定义的, 他是函数体所处的一个环境, 是一个外部的变量, 外面的这个变量sum叫做自由变量.

    编译器就会连一根线, 连到sum里面去, 我们这里面的sum是一个int, 他可能是结构, 然后继续连下去, 最后组成了一棵树, 我们不断的找这种连接关系, 最终, 会吧所有需要连接的东西连完. 全部连完以后, 我们这个东西就叫闭包.

    当函数返回的时候, 返回的是一个闭包 return func,  不是返回了一段代码,而是返回了函数以及对sum的引用, 并且sum变量会被保存下来, 保存到函数里面去.

    2, go语言闭包的案例

    • 斐波那契数列
      package main
      
      import "fmt"
      
      // 1 1 2 3 5 8 13 21
      //   x y
      //     x y
      func feibonaqi() func() int {
          x, y := 0, 1          // 自由变量
          return func() int {        // 闭包, 闭包会保存自由变量的值
              x, y = y, x + y
              return x
          }
      }
      
      func main() {
          fmt.Println("斐波那契数列")
          f := feibonaqi()
          fmt.Println(f()) //1
          fmt.Println(f()) //1
          fmt.Println(f()) //2
          fmt.Println(f()) //3
          fmt.Println(f()) //5
          fmt.Println(f()) //8
          fmt.Println(f()) //13
      }

      使用了闭包保存了自由变量的值.

    • 为函数实现接口
      package main
      
      import (
          "bufio"
          "fmt"
          "io"
          "strings"
      )
      
      // 1 1 2 3 5 8 13 21
      //   x y
      //     x y
      func feibonaqi() func() int {
          x, y := 0, 1          // 自由变量
          return func() int {        // 闭包, 闭包会保存自由变量的值
              x, y = y, x + y
              return x
          }
      }
      
      type fbnqGen func() int
      
      func (f fbnqGen) Read(p []byte) (n int, err error) {
          next := f()
          if next > 10000 {
              return 0, io.EOF
          }
          s := fmt.Sprintf("%d 
      ", next)
          return strings.NewReader(s).Read(p)
      }
      
      // 我们之前打印文件中的内容
      func printFileContent(r io.Reader) {
          scanner := bufio.NewScanner(r)
      
          for scanner.Scan()  {
              fmt.Println(scanner.Text())
          }
      
      }
      
      func main() {
          fmt.Println("斐波那契数列")
          //f := feibonaqi()
          // 以下就是一个打印的功能, 我们把这一段封装以下, 向文件一样封装, 然后打印
          /*fmt.Println(f()) //1
          fmt.Println(f()) //1
          fmt.Println(f()) //2
          fmt.Println(f()) //3
          fmt.Println(f()) //5
          fmt.Println(f()) //8
          fmt.Println(f()) //13*/
          
          // 使用的时候, feibonaqi是一个fbnqGen类型, 所以, 可以直接当做fbnqGen来使用
          var f fbnqGen
          f = feibonaqi()
          printFileContent(f)
      }
    • 使用函数来遍历二叉树

      之前做的二叉树是只能打印二叉树的元素

      

    package tree
    
    import "fmt"
    
    type TreeNode struct {
        Value int
        Left, Right *TreeNode
    }
    
    func  NewTreeNode(value int) *TreeNode {
        return &TreeNode{Value:value}
    }
    
    func (node *TreeNode) Print() {
        if node == nil {
            fmt.Println("node为空指针")
        }
        fmt.Println(node.Value)
    }
    
    func (node *TreeNode) SetValue() {
        node.Value = 200
    }
    
    func(node *TreeNode) Traveres() {
        if node == nil{
            return
        }
        node.Left.Traveres()
        node.Print()
        node.Right.Traveres()
    }
    
    
    func main()  {
        //创建结构体的方法
        var root TreeNode
        root = TreeNode{Value:3}
        root.Left = &TreeNode{}
        root.Right = &TreeNode{5, nil, nil}
        root.Left.Left = new(TreeNode)
        root.Right.Right = NewTreeNode(4)
    
        root.Traveres()
    
        var node *TreeNode
        node.Traveres()
    
    }

    返回值是 0 0 3 5 4

    这里只能打印树节点的值, 那么还想要做其他的事, 怎么办呢? 如果扩展这个方法呢? 其实后面想要做的事有很多, 但是现在我也不确定要做哪些

    package tree
    
    import (
        "fmt"
    )
    
    type TreeNode struct {
        Value int
        Left, Right *TreeNode
    }
    
    func  NewTreeNode(value int) *TreeNode {
        return &TreeNode{Value:value}
    }
    
    func (node *TreeNode) Print() {
        if node == nil {
            fmt.Println("node为空指针")
        }
        fmt.Println(node.Value)
    }
    
    func (node *TreeNode) SetValue() {
        node.Value = 200
    }
    
    func(node *TreeNode) Traveres() {
        node.TraveresFunc(func(n *TreeNode) {
            n.Print()
        })
        fmt.Println()
    }
    
    func (node *TreeNode) TraveresFunc(f func(*TreeNode)) {
        if node == nil{
            return
        }
        node.Left.TraveresFunc(f)
        f(node)
        node.Right.TraveresFunc(f)
    }
    
    
    func main()  {
        //创建结构体的方法
        var root TreeNode
        root = TreeNode{Value:3}
        root.Left = &TreeNode{}
        root.Right = &TreeNode{5, nil, nil}
        root.Left.Left = new(TreeNode)
        root.Right.Right = NewTreeNode(4)
    
        root.Traveres()
    
        var node *TreeNode
        node.Traveres()
    
    }

    增加了一个函数: 左序遍历. 但是遍历后的值如何处理呢?

    func (node *TreeNode) TraveresFunc(f func(*TreeNode)) {
        if node == nil{
            return
        }
        node.Left.TraveresFunc(f)
        f(node)
        node.Right.TraveresFunc(f)
    }

    只做遍历, 不做处理. 具体的处理方法, 由处理的函数实现. 比如要打印遍历后的值

    func(node *TreeNode) Traveres() {
        node.TraveresFunc(func(n *TreeNode) {
            n.Print()
        })
        fmt.Println()
    }

    在比如, 我要统计元素个数

    func(node *TreeNode) Count() {
        sum := 0
        node.TraveresFunc(func(n *TreeNode) {
            sum ++
        })
        fmt.Println(sum)
    }

    这样处理, 整个函数就灵活的多了. 

    下面贴出完整的代码

    package main
    
    import (
        "fmt"
    )
    
    type TreeNode struct {
        Value int
        Left, Right *TreeNode
    }
    
    func  NewTreeNode(value int) *TreeNode {
        return &TreeNode{Value:value}
    }
    
    func (node *TreeNode) Print() {
        if node == nil {
            fmt.Println("node为空指针")
        }
        fmt.Println(node.Value)
    }
    
    func (node *TreeNode) SetValue() {
        node.Value = 200
    }
    
    func(node *TreeNode) Traveres() {
        node.TraveresFunc(func(n *TreeNode) {
            n.Print()
        })
        fmt.Println()
    }
    
    func(node *TreeNode) Count() {
        sum := 0
        node.TraveresFunc(func(n *TreeNode) {
            sum ++
        })
        fmt.Println(sum)
    }
    
    func (node *TreeNode) TraveresFunc(f func(*TreeNode)) {
        if node == nil{
            return
        }
        node.Left.TraveresFunc(f)
        f(node)
        node.Right.TraveresFunc(f)
    }
    
    
    
    func main()  {
        //创建结构体的方法
        var root TreeNode
        root = TreeNode{Value:3}
        root.Left = &TreeNode{}
        root.Right = &TreeNode{5, nil, nil}
        root.Left.Left = new(TreeNode)
        root.Right.Right = NewTreeNode(4)
    
        root.Traveres()
            root.Count()
    
    }
        

    总结:


    参考文章: 

    1. https://blog.csdn.net/jadeshu/article/details/102896843

    2. https://www.cnblogs.com/ycx95/p/9362175.html

  • 相关阅读:
    自己回答自己的问题
    作业5 四则运算 测试与封装 5.1 5.2
    2015430 加法阶段一的封装测试
    20150423 提问2
    Java 泛型中的<? super T>和<? extends T>
    ssh 查看端口占用
    Java 回调
    SpringMVC4+MyBaits3配置
    Selenium 远程执行和监听类
    Intellij idea主题
  • 原文地址:https://www.cnblogs.com/ITPower/p/12306172.html
Copyright © 2020-2023  润新知