map reduce functional programming

14
Map Reduce

Upload: tusjain

Post on 22-May-2015

61 views

Category:

Technology


0 download

DESCRIPTION

A brief introduction of Functional programming using Map Reduce algorithm.

TRANSCRIPT

Page 1: Map reduce   functional programming

Map Reduce

Page 2: Map reduce   functional programming

Map

Page 3: Map reduce   functional programming

Double the contents of an array

var a = [1, 2, 3];

for (int I = 0; I < a.length; i++){

a[ i ] = 2*a[ i ];

}

Output:

a = [2, 4, 6];

Page 4: Map reduce   functional programming

Double the contents of an array

var a = [1, 2, 3];

for (int i = 0; i < a.length; i++){

a[ i ] = 2*a[ i ];}

var a = [1, 2, 3];

for (int i = 0; i < a.length; i++){

a[ i ] = fn(a[ i ]);}

where fn(x){ return 2*x;}

Page 5: Map reduce   functional programming

Double the contents of an array

var a = [1, 2, 3];

for (int i = 0; i < a.length; i++){

a[ i ] = fn(a[ i ]);

} where fn(x){ return 2*x;}

function map(fn, a){

for (int i = 0; i < a.length; i++){

a[ i ] = fn(a[ i ]);

}where fn(x){ return 2*x;}

Page 6: Map reduce   functional programming

Double the contents of an array

function map(fn, a){

for (int i = 0; i < a.length; i++){

a[ i ] = fn(a[ i ]);

}where fn(x){ return 2*x;}

map(function(x){ return 2*x;},a}

General Form

Page 7: Map reduce   functional programming

Reduce

Page 8: Map reduce   functional programming

Sum the contents of an array

function sum(a){

var s= 0;

for(int i=0; i<a.length; i++){

s = s + a[ i ];

}

return s;

}

Page 9: Map reduce   functional programming

Sum the contents of an arrayfunction sum(a){

var s= 0;

for(int i=0; i<a.length; i++){

s = s + a[ I ]; }

return s;

}

function sum(a){

var s= 0;

for(int i=0; i<a.length; i++){

s = fn(s, a[ I ]); }

return s;

}

where fn(a, b){ return a+b;}

Page 10: Map reduce   functional programming

Sum the contents of an arrayfunction sum(a){

var s= 0;

for(int i=0; i<a.length; i++){

s = fn(s, a[ I ]);

}

return s;

}where fn(a, b){ return a+b;}

function reduce(fn, a, init){

var s= init;

for(int i=0; i<a.length; i++){

s = fn(s, a[ I ]);

}

return s;

}

where fn(a, b){ return a+b;}

Page 11: Map reduce   functional programming

Sum the contents of an arrayfunction reduce(fn, a, init){

var s= init;

for(int i=0; i<a.length; i++){

s = fn(s, a[ I ]);

}

return s;

} where fn(a, b){ return a+b;}

General form

reduce(function(a,b){ retun a+b;}, a, init);

Page 12: Map reduce   functional programming

Benefits

In case of big arrays, code running on multiple machines, one need not to rewrite the code for computing but just replace the implementation of map and reduce as required.

Microsoft Word Document

map(function(x){ return 2*x;},a}

reduce(function(a,b){ return a+b;}, a, init);

Page 13: Map reduce   functional programming

What is hadoop?

Hadoop is parallel, distributive, fault tolerant and network topology aware implementation of MapReduce algorithm.

Page 14: Map reduce   functional programming