Golang 高阶函数
定义
高阶函数是接收函数作为参数或返回函数作为输出的函数。
高阶函数是对其他函数进行操作的函数,要么将它们作为参数,要么返回它们。
举例
函数作为参数
package main
import "fmt"
func sum(x, y int) int {
return x + y
}
func process(x int, y int, f func(int, int) int) int {
return f(x, y)
}
func main() {
x, y := 1, 2
fmt.Println(process(x, y, sum))
}
输出结果
3
函数作为返回值
package main
import "fmt"
func addPrefix(str string) string {
return "prefix_" + str
}
func addSuffix(str string) string {
return str + "_suffix"
}
func process(idx int) func(string) string {
switch idx {
case 1:
return addPrefix
case 2:
return addSuffix
default:
return addPrefix
}
}
func main() {
need_prefix := []string{"aaa", "bbb", "ccc"}
need_suffix := []string{"AAA", "BBB", "CCC"}
prefix_f := process(1)
suffix_f := process(2)
for i, v := range need_prefix {
need_prefix[i] = prefix_f(v)
}
for i, v := range need_suffix {
need_suffix[i] = suffix_f(v)
}
fmt.Println("need_prefix:", need_prefix)
fmt.Println("need_suffix:", need_suffix)
}
输出结果
need_prefix: [prefix_aaa prefix_bbb prefix_ccc]
need_suffix: [AAA_suffix BBB_suffix CCC_suffix]
两者结合起来
衣服,交通工具,水果分类
定义一个结构体
type Commodity struct {
Name string
Type string
}
type Cds []*Commodity
自定义结构体的String()
先看一下没有自定义结构体的String(),打印出来的结果
[0xc0000ba000 0xc0000ba020 0xc0000ba040 0xc0000ba060 0xc0000ba080 0xc0000ba0a0]
map[clothes:[] fruit:[] others:[] transportation:[]]
map[clothes:[0xc0000ba080 0xc0000ba0a0] fruit:[0xc0000ba000 0xc0000ba020] others:[] transportation:[0xc0000ba040 0xc0000ba060]]
all fruit: [0xc0000ba000 0xc0000ba020]
全都是地址,所以我们需要实现自定义结构体的String()
func (cd *Commodity) String() string {
return cd.Type + "," + cd.Name
}
func (cds Cds) String() string {
var result string
for _, v := range cds {
// result = append(result, v.String()...)
result += v.String() + " | "
}
return result
}
再看下打印结果,顺眼多了
fruit,apple | fruit,banana | transportation,car | transportation,bike | clothes,T-shirt | clothes,skirt |
map[clothes: fruit: others: transportation:]
map[clothes:clothes,T-shirt | clothes,skirt | fruit:fruit,apple | fruit,banana | others: transportation:transportation,car | transportation,bike | ]
all fruit: fruit,apple | fruit,banana |
定一个函数,参数是用来处理每一个结构体函数,这样可以省去我们多次的for遍历
func (cds Cds) process(f func(cd *Commodity)) {
for _, v := range cds {
f(v)
}
}
正常情况下我们的输入是一串没有区分类别的数据,这种情况下我们肯定是需要去做分类的,所以我们定义个自动分类的函数
func classifier(types []string) (func(cd *Commodity), map[string]Cds) {
result := make(map[string]Cds)
for _, v := range types {
result[v] = make(Cds, 0)
}
result["others"] = make(Cds, 0)
appender := func(cd *Commodity) {
if _, ok := result[cd.Type]; ok {
result[cd.Type] = append(result[cd.Type], cd)
} else {
result["others"] = append(result["others"], cd)
}
}
return appender, result
}
除了分类后,我们有时候可能需要在一大堆未分类的数据中找到我们需要的某一种类型,所以我们还需要定义一个查找函数
func (cds Cds) findType(f func(cd *Commodity) bool) Cds {
result := make(Cds, 0)
cds.process(func(c *Commodity) {
if f(c) {
result = append(result, c)
}
})
return result
}
完整代码
package main
import "fmt"
type Commodity struct {
Name string
Type string
}
type Cds []*Commodity
func (cd *Commodity) String() string {
return cd.Type + "," + cd.Name
}
func (cds Cds) String() string {
var result string
for _, v := range cds {
// result = append(result, v.String()...)
result += v.String() + " | "
}
return result
}
func main() {
fruit1 := Commodity{"apple", "fruit"}
fruit2 := Commodity{"banana", "fruit"}
transportation1 := Commodity{"car", "transportation"}
transportation2 := Commodity{"bike", "transportation"}
clothes1 := Commodity{"T-shirt", "clothes"}
clothes2 := Commodity{"skirt", "clothes"}
all_commodity := Cds([]*Commodity{&fruit1, &fruit2,
&transportation1, &transportation2,
&clothes1, &clothes2})
fmt.Println(all_commodity)
types := []string{"fruit", "transportation", "clothes"}
appender, cds := classifier(types)
fmt.Println(cds)
all_commodity.process(appender)
fmt.Println(cds)
all_fruit := all_commodity.findType(func(cd *Commodity) bool {
return cd.Type == "fruit"
})
fmt.Println("all fruit:", all_fruit)
}
func (cds Cds) process(f func(cd *Commodity)) {
for _, v := range cds {
f(v)
}
}
func (cds Cds) findType(f func(cd *Commodity) bool) Cds {
result := make(Cds, 0)
cds.process(func(c *Commodity) {
if f(c) {
result = append(result, c)
}
})
return result
}
func classifier(types []string) (func(cd *Commodity), map[string]Cds) {
result := make(map[string]Cds)
for _, v := range types {
result[v] = make(Cds, 0)
}
result["others"] = make(Cds, 0)
appender := func(cd *Commodity) {
if _, ok := result[cd.Type]; ok {
result[cd.Type] = append(result[cd.Type], cd)
} else {
result["others"] = append(result["others"], cd)
}
}
return appender, result
}
输出结果
fruit,apple | fruit,banana | transportation,car | transportation,bike | clothes,T-shirt | clothes,skirt |
map[clothes: fruit: others: transportation:]
map[clothes:clothes,T-shirt | clothes,skirt | fruit:fruit,apple | fruit,banana | others: transportation:transportation,car | transportation,bike | ]
all fruit: fruit,apple | fruit,banana |
Golang 高阶函数的更多相关文章
- c#语言-高阶函数
介绍 如果说函数是程序中的基本模块,代码段,那高阶函数就是函数的高阶(级)版本,其基本定义如下: 函数自身接受一个或多个函数作为输入. 函数自身能输出一个函数,即函数生产函数. 满足其中一个条件就可以 ...
- swift 的高阶函数的使用代码
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- JavaScript高阶函数
所谓高阶函数(higher-order function) 就是操作函数的函数,它接收一个或多个函数作为参数,并返回一个新函数. 下面的例子接收两个函数f()和g(),并返回一个新的函数用以计算f(g ...
- python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))
1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...
- python学习道路(day4note)(函数,形参实参位置参数匿名参数,匿名函数,高阶函数,镶嵌函数)
1.函数 2种编程方法 关键词面向对象:华山派 --->> 类----->class面向过程:少林派 -->> 过程--->def 函数式编程:逍遥派 --> ...
- Scala的函数,高阶函数,隐式转换
1.介绍 2.函数值复制给变量 3.案例 在前面的博客中,可以看到这个案例,关于函数的讲解的位置,缺省. 4.简单的匿名函数 5.将函数做为参数传递给另一个函数 6.函数作为输出值 7.类型推断 8. ...
- Python之路 day3 高阶函数
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa """ 变量可以指向函数,函数的参数能接收变量, 那么 ...
- JavaScript高阶函数 map reduce filter sort
本文是笔者在看廖雪峰老师JavaScript教程时的个人总结 高阶函数 一个函数就接收另一个函数作为参数,这种函数就称之为高阶函数 1.高阶函数之map: ...
- js高阶函数
我是一个对js还不是很精通的选手: 关于高阶函数详细的解释 一个高阶函数需要满足的条件(任选其一即可) 1:函数可以作为参数被传递 2:函数可以作为返回值输出 吧函数作为参数传递,这代表我们可以抽离一 ...
随机推荐
- Oracle 11G DBMS包和类型参考
参阅:https://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_lob.htm#ARPLS66712
- python模块相互依赖的解决方案
第一种:将相互依赖的文件中的其中一个文件的代码移植到另一个文件中... 第二种:将 import .... 或 from ... import 语句的位置移动到def函数内部,由于import和fro ...
- 如何确保消息正确地发送至 RabbitMQ? 如何确保消息接收方消费了消息?
发送方确认模式 将信道设置成 confirm 模式(发送方确认模式),则所有在信道上发布的消息都 会被指派一个唯一的 ID. 一旦消息被投递到目的队列后,或者消息被写入磁盘后(可持久化的消息),信 道 ...
- idea使用maven工程创建web项目并支持jsp
主要要再pom文件里面添加依赖: <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> & ...
- 学习Solr(一)
一.安装 1.需要的安装包:apache-tomcat-7.0.47.tar.gz.solr-4.10.3.tgz.tgz(jdk自行安装) 2.解压tomcat并创建solr文件夹 [root@lo ...
- 时间工具类之"获取相差天数"
一.时间工具类DateUtils之"获取相差天数" 1 /** 2 * 相差天数 3 * 4 * <p>TODO 方法功能描述 5 * 6 * @param start ...
- MM32F0020 UART1硬件自动波特率的使用
目录: 1.MM32F0020简介 2.UART自动波特率校准应用场景 3.MM32F0020 UART自动波特率校准原理简介 4.MM32F0020 UART1 NVIC硬件自动波特率配置以及初始化 ...
- Altium Designer PCB文件的绘制(下:PCB布线和检查)
在完成电路板的布局工作后,就可以开始布线操作了.在PCB的设计中,布线是完成产品设计的最重要的步骤,其要求最高.技术最细.工作量最大.PCB布线可分为单面布线.双面布线.多层布线.布线的方式有自动布线 ...
- Shiro 安全框架详解一(概念+登录案例实现)
shiro 安全框架详细教程 总结内容 一.RBAC 的概念 二.两种常用的权限管理框架 1. Apache Shiro 2. Spring Security 3. Shiro 和 Spring Se ...
- 单例设计模式(Singleton)
一.单例设计模式介绍 所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例, 并且该类只提供一个取得其对象实例的方法(静态方法) 例如:Hibernate的Se ...