Golang - 对比Python高阶函数之MapReduce

如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clusters”,你就能大概明白map/reduce的概念。

Map

func maps(f func(int) int, nums []int) []int {
	var ret = make([]int, len(nums))
	for i, n := range nums {
		ret[i] = f(n)
	}
	return ret
}

func f(x int) int {
	return x + 2
}

var nums = []int{1, 2, 3, 4, 5}
fmt.Println(maps(f, nums))
// [3 4 5 6 7]

VS

map(lambda x: x + 2, [1, 2, 3, 4, 5])
# 注意:在python3中map()函数返回map object,而非python2直接返回list对象
list(map(lambda x: x + 2, [1, 2, 3, 4, 5])) # 转为list
# 3, 4, 5, 6, 7

Reduce

func reduces(f func(x, y int) int, nums []int) int {
	var sum = 0
	for _, n := range nums {
		sum = f(sum, n)
	}
	return sum
}

func f1(x, y int) int {
	return x + y
}

func f2(x, y int) int {
	return x*10 + y
}

var nums = []int{1, 2, 3, 4, 5}
fmt.Println(reduces(f1, nums))
// 15
fmt.Println(reduces(f2, nums))
// 12345

VS

from functools import reduce
# 注意:在python3中reduce()函数已经不再是内置函数,而是放到了functools包下面
reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
# 15
reduce(lambda x, y: x * 10 + y, [1, 2, 3, 4, 5])
# 12345

Ref


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!