# 从Map和Reduce说起

>>> map(lambda x: x*x,[1,2,3,4,5]) [1, 4, 9, 16, 25] >>> reduce((lambda x,y:x+y),[1,2,3,4,5]) 15
map将过程x*x应用于list中的每个元素,并产生新的list作为返回值,而reduce每次将 list中当前元素与上次应用过程的结果作为输入应用到过程中. 看一个复杂的例子,用于求前10个Fibobacci数:
map(lambda x,f=lambda x,f:(x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),range(10))
Map与Reduce在Scheme/Lisp中就已经流行,Scheme中的Map实现为:
(define (map proc items)    	(if (null? items)  (list )        	(cons (proc (car items)) (map proc (cdr items)))))  (define (reduce proc items)   (if (null? items)  0       	(proc (car items) (reduce proc (cdr items)))))

(reduce (lambda (x y) (+ x y)) (map (lambda(x) (* x x)) (list 1 2 3 4 5)))

#include<stdio.h>  typedef int(*function)(int); typedef int(*reduceFunc)(int,int);  int square(int i) { 		return i*i; }  int multiply(int i) { 		return 2*i; }  int add(int i, int j) { 		return i + j; }  int map(function func,int* list,int len) { 		for(int i=0;i&ltlen; i++) 				list[i] = func(list[i]); 		return len; }  int reduce(reduceFunc func,int* list,int len) { 		if(len<=0) 				return -1; 		int retVal = list[0]; 		for(int i=1; i<len; i++) 				retVal = func(list[i],retVal); 	 		return retVal; }  int main(int argc, char** argv) { 		int intArray[5]; 		for(int i=0;i<5;i++) 		{ 				intArray[i] = i+1;  		} 	 		function funcPointer = (function)&square; 		map(funcPointer,intArray,5); 		for(int i=0;i<5; i++) 	{ 				printf("intArray[i]:%d/n ",intArray[i]); 		}  		funcPointer = (function)&multiply; 		map(funcPointer,intArray,5); 		for(int i=0;i<5; i++) 		{ 				printf("intArray[i]:%d/n ",intArray[i]); 		}  		int reduceArray[] = {8,7,2,-17,5,-5}; 		reduceFunc reduceFp = (reduceFunc)&add; 		int reduceValue = reduce(reduceFp,reduceArray,6); 		printf("reduceValue: %d/n",reduceValue); }
Gooogle的工程师们根据Map/Reduce的思想,实现了一种分布式调度算法MapReduce,其核心是利用一个Map操作以Key/Value对作为输入,并且产生一组中间key/Value集合。Reduce方法接收一个中间的键I和一组关于该键的值,然后将该将这些值合并成一组更小的集合.

#### 简单说说MapReduce, Tez, Spark

2015-01-25 09:09:01

#### Map和Reduce中间过程描述

2016-09-30 16:59:59

#### 什么是Map/Reduce

2015-02-03 15:20:22

#### Mapreduce学习指导及疑难解惑汇总

2014-08-31 16:04:33

2015-07-05 20:37:19

#### mapreduce的理解

2016-08-18 16:25:29

2015-01-09 16:58:20

#### MapReduce面试题1

2018-05-11 13:23:56