Golang函数式编程深入分析实例

 更新时间:2023年01月10日 11:00:24   作者:梦想画家  
习惯与函数式编程语言的开发者,会认为for循环和if判断语句是冗长的代码,通过使用map和filter处理集合元素让代码更可读。本文介绍Go闭包实现集合转换和过滤功能

定义集合功能函数

首先定义用于测试的结构体WorkWith

// WorkWith is the struct we'll
// be implementing collections for
type WorkWith struct {
	Data    string
	Version int
}

针对该结构体定义filter和map函数:

// 基于判断函数过滤集合,返回符合条件的集合元素
func Filter(ws []WorkWith, f func(w WorkWith) bool) []WorkWith {
	// depending on results, smaller size for result
	// is len == 0
	result := make([]WorkWith, 0)
	for _, w := range ws {
		if f(w) {
			result = append(result, w)
		}
	}
	return result
}
// 基于转换函数转换集合元素,返回集合的元素为转换后的元素
func Map(ws []WorkWith, f func(w WorkWith) WorkWith) []WorkWith {
	// the result should always be the same
	// length
	result := make([]WorkWith, len(ws))
	for pos, w := range ws {
		newW := f(w)
		result[pos] = newW
	}
	return result
}

实现具体功能函数

import "strings"
// LowerCaseData does a ToLower to the
// Data string of a WorkWith
func LowerCaseData(w WorkWith) WorkWith {
	w.Data = strings.ToLower(w.Data)
	return w
}
// IncrementVersion increments a WorkWiths
// Version
func IncrementVersion(w WorkWith) WorkWith {
	w.Version++
	return w
}
// OldVersion returns a closures
// that validates the version is greater than
// the specified amount
func OldVersion(v int) func(w WorkWith) bool {
	return func(w WorkWith) bool {
		return w.Version >= v
	}
}

上面定义了三个函数,LowerCaseData修改WorkWith中Data值为小写形式,IncrementVersion让WorkWith中版本增加1,OldVersion基于参数过滤版本。

测试集合功能

定义测试用例文件:

import (
	"fmt"
	"testing"
)
func TestMap(t *testing.T) {
	ws := []WorkWith{
		{"Example", 1},
		{"Example 2", 2},
	}
	fmt.Printf("Initial list: %#v\n", ws)
	// first lower case the list
	ws = Map(ws, LowerCaseData)
	fmt.Printf("After LowerCaseData Map: %#v\n", ws)
	// next increment all versions
	ws = Map(ws, IncrementVersion)
	fmt.Printf("After IncrementVersion Map: %#v\n", ws)
	// lastly remove all versions older than 3
	ws = Filter(ws, OldVersion(3))
	fmt.Printf("After OldVersion Filter: %#v\n", ws)
}

运行 go test . -v

输出结果如下:

Initial list: []collections.WorkWith{collections.WorkWith{Data:"Example", Version:1}, collections.WorkWith{Data:"Example 2", Version:2}}

After LowerCaseData Map: []collections.WorkWith{collections.WorkWith{Data:"example", Version:1}, collections.WorkWith{Data:"example 2", Version:2}}

After IncrementVersion Map: []collections.WorkWith{collections.WorkWith{Data:"example", Version:2}, collections.WorkWith{Data:"example 2", Version:3}}

After OldVersion Filter: []collections.WorkWith{collections.WorkWith{Data:"example 2", Version:3}}

上面示例中,我们注意到函数都没有返回任何error对象,这遵循函数式编程思想,尽可能让函数纯粹:不修改原集合元素,即对原集合无副作用,而是生成新的集合。如果需要对集合应用多个功能,那么这种模式能够省去很多麻烦,并且测试也很简单。我们还可以将映射和过滤器链接在一起,让代码更简洁可读。

	ws := []WorkWith{
		{"Example", 1},
		{"Example 2", 2},
	}
	fmt.Printf("Initial list: %#v\n", ws)
	result := Filter(Map(Map(ws, LowerCaseData), IncrementVersion), OldVersion(3))
	fmt.Printf("After OldVersion Filter: %#v\n", result)

如果功能函数定义为集合类型的方法,并返回集合类型,则上述代码会更优雅。

泛型实现

上面代码仅能在特定类型上使用,我们自然想实现泛型函数,下面通过一个简单示例进行说明:

func map2[T, U any](data []T, f func(T) U) []U {
    res := make([]U, 0, len(data))
    for _, e := range data {
        res = append(res, f(e))
    }
    return res
}

该函数接收类型T,转换后返回类型U,当然两者类型也可以一样。下面测试函数功能:

    // 字符串转大写
    words := []string{"war", "cup", "water", "tree", "storm"}
    result := map2(words, func(s string) string {
        return strings.ToUpper(s)
    })
    fmt.Println(result)
    // 生成原集合元素的平方集合
    fmt.Println("-------------------")
    numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    squares := map2(numbers, func(n int) int {
        return n * n
    })
    fmt.Println(squares)
    // 数值转为字符串
    fmt.Println("-------------------")
    as_strings := map2(numbers, func(n int) string {
        return strconv.Itoa(n)
    })
    fmt.Printf("%q", as_strings)

到此这篇关于Golang函数式编程深入分析实例的文章就介绍到这了,更多相关Go函数式编程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • CSP communicating sequential processes并发模型

    CSP communicating sequential processes并发模型

    这篇文章主要为大家介绍了CSP communicating sequential processes并发模型,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Golang爬虫框架 colly的使用

    Golang爬虫框架 colly的使用

    本文主要介绍了Golang爬虫框架 colly的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • VScode下配置Go语言开发环境(2023最新)

    VScode下配置Go语言开发环境(2023最新)

    在VSCode中配置Golang开发环境是非常简单的,本文主要记录了Go的安装,以及给vscode配置Go的环境,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • Go 常量基础概念(声明更改只读)

    Go 常量基础概念(声明更改只读)

    这篇文章主要为大家介绍了Go常量基础概念包括常量的声明更改只读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • go chan基本使用详解

    go chan基本使用详解

    本文主要介绍了go chan基本使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • 搭建Go语言的ORM框架Gorm的具体步骤(从Java到go)

    搭建Go语言的ORM框架Gorm的具体步骤(从Java到go)

    很多朋友不知道如何使用Goland软件,搭建一个ORM框架GORM,今天小编给大家分享一篇教程关于搭建Go语言的ORM框架Gorm的具体步骤(从Java到go),感兴趣的朋友跟随小编一起学习下吧
    2022-09-09
  • go基础语法50问及方法详解

    go基础语法50问及方法详解

    这篇文章主要为大家介绍了go基础语法50问及方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • 解决 Golang VS Code 插件下载安装失败的问题

    解决 Golang VS Code 插件下载安装失败的问题

    这篇文章主要介绍了解决 Golang VS Code 插件下载安装失败,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • golang语言中for循环语句用法实例

    golang语言中for循环语句用法实例

    这篇文章主要介绍了golang语言中for循环语句用法,实例分析了for循环遍历的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • golang 调用c语言动态库方式实现

    golang 调用c语言动态库方式实现

    本文主要介绍了golang 调用c语言动态库方式实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12

最新评论