SlideShare a Scribd company logo
Let’s talk about lambda
Why care?
NULL is troublesome	

The dog breeder example
06 Map Reduce
var colleen, richard;	
!

colleen = {	
pet: {	
mother: {	
owner: "Sarah"	
}	
}	
};	
!

richard = {	
pet: null	
};
Given a person, return its pet’s
mother’s owner.
function findBreeder(person) {	
return person.pet.mother.owner;	
}
function findBreeder(person) {	
return person.pet.mother.owner;	
}	
!

>>> findBreeder(colleen);	
"Sarah"	
!

>>> findBreeder(richard);	
Exception: "Can not read property 'mother' of
undefined"
function findBreeder(person) {	
if (!person) {	
return null;	
}	
!

if (!person.pet) {	
return null;	
}	
	
if (!person.pet.mother) {	
return null;	
}	
!

return person.pet.mother.owner;	
}
>>> findBreeder(colleen);	
"Sarah"	
!

>>> findBreeder(richard);	
null
LISTS are troublesome	

The investment broker example
Broker

Clients

Investment Types

Markets

Balance
var colin = {	
clients: [	
{	
name: "Fred",	
investments: [	
{	
name: "Shares",	
markets: [	
{	
name: "Japan",	
value: 7000	
},	
{	
name: "America",	
value: 8888	
}	
]	
},	
{	
name: "Gold",	
markets: [	
{	
name: "China",	
value: 3333	
},	
]	
}	
]	
}	
]	
};
Find the sum of all of an
investors’s client’s investments.
sumInvestments = (clients) ->	
values = []	
foreach client in clients:	
foreach investment in client.investments	
foreach market in investment.markets	
values.push(market.balance)	
return sum(values)
function sumInvestments (clients) {	
var i, j, k;	
var investments, markets;	
var value = 0;	
!
for (i = 0; i < clients.length; i++) {	
investments = clients[i].investments;	
!
for (j = 0; j < investments.length; j++) {	
markets = investments[j].markets;	
!
for (k = 0; k < markets.length; k++) {	
value += markets[k].value	
}	
}	
}	
!
return value;	
};
06 Map Reduce
(multi-layer list iteration) vs. (null checking)	

!

What’s the common thread?
no code duplication	

!

lots of concept duplication
function call_unless_null(val, fn) {	
if (val !== null) {	
return fn(val);	
}	

!
return null;	
}	

!
function findBreeder(person) {	
return call_unless_null(person, function(person) {	
return call_unless_null(person.pet, function(pet) {	
return call_unless_null(pet.mother, function(mother) {	
return mother.owner;	
});	
});	
});	
}
>>> findBreeder(colleen);	
"Sarah"	
!

>>> findBreeder(richard);	
null
Still too much duplication.
function reduce(iter, memo, fn) {	
for (var i = 0; i < iter.length; i++) {	
memo = fn(memo, iter[i]);	
}	

!
return memo;	
}	

!
function get_unless_null(obj, key) {	
if (obj !== null && obj !== undefined) {	
return obj[key];	
}	
	
return null;	
}	

!
function findBreeder(person) {	
return reduce(['pet', 'mother', ‘owner’], person, get_unless_null);	
}
>>> findBreeder(colleen);	
"Sarah"	
!

>>> findBreeder(richard);	
null
function sumInvestments(clients) {	
var values;	

!
values = flat_map(clients, function(client) {	
return flat_map(client.investments, function(investment) {	
return flat_map(investment.markets, function(market) {	
return market.value;	
});	
});	
});	

!
return sum(values);	
};
function map(iter, fn) {	
var out = [],	
val;	

!
for (var i = 0; i < iter.length; i++) {	
out[i] = fn(iter[i]);	
}	

!
return out;	
}	

!
function flat_map(iter, fn) {	
iter = map(iter, fn);	
return reduce(iter, [], function(memo, item) {	
return memo.concat(item);	
});	
}	

!
function sum(list) {	
return reduce(list, 0, function(memo, num) {	
return memo + num;	
});	
}
function sumInvestments(clients) {	
var v, fn;	

!
fn = function(arr, key) {	
return flat_map(arr, function(item) {	
return get_unless_null(item, key);	
});	
};	

!
v = reduce(clients, ['investments', 'markets', 'value'], fn);	
return sum(v);	
};
What have we accomplished?
function sumInvestments (clients) {	
var i, j, k;	
var investments, markets;	
var value = 0;	

!
!

ugly

for (i = 0; i < clients.length; i++) {	
investments = clients[i].investments;	
for (j = 0; j < investments.length; j++) {	
markets = investments[j].markets;	

!

for (k = 0; k < markets.length; k++) {	
value += markets[k].value	
}	
}	

!

}	
return value	

};	

!

function sumInvestments(clients) {	
var v;	

!

!
};

awesome

v = reduce(clients, ['investments', 'markets', 'value'], function(arr, key) {	
return flat_map(arr, function(item) {	
return item[key];	
});	
});	
return sum(v);
ugly
function findBreeder(person) {	
if (!person) {	
return null;	
}	

!
if (!person.pet) {	
return null;	
}	
	
if (!person.pet.mother) {	
return null;	
}	

!
return person.pet.mother.owner;	
}	

!
!
!

awesome

function findBreeder(person) {	
return reduce(get_unless_null, person, ['pet', 'mother', 'owner']);	
}
Concept duplication is just
masked code duplication
Repeating patterns are hint
Higher-order functions make all
this possible

More Related Content

PDF
Learn You a Functional JavaScript for Great Good
PDF
Excellent
PDF
Rails is not just Ruby
PPTX
Metaprogramming in ES6
PDF
Your code sucks, let's fix it! - php|tek13
PDF
05 Web Services
PDF
04 Advanced Javascript
Learn You a Functional JavaScript for Great Good
Excellent
Rails is not just Ruby
Metaprogramming in ES6
Your code sucks, let's fix it! - php|tek13
05 Web Services
04 Advanced Javascript

Similar to 06 Map Reduce (20)

PPTX
Understanding Prototypal Inheritance
DOC
Jsphp 110312161301-phpapp02
PDF
Tactical DDD patterns in Go
PPTX
Elegant objects
PDF
Object-oriented Javascript
PDF
ScotRuby - Dark side of ruby
PDF
JavaScript for PHP developers
PPTX
A Few Interesting Things in Apple's Swift Programming Language
PDF
Your code sucks, let's fix it
PPTX
Benefits of Kotlin
PDF
Your code sucks, let's fix it
PDF
Google Guava - Core libraries for Java & Android
PDF
Javascript ES6 generators
PPTX
JavaScript - i och utanför webbläsaren (2010-03-03)
PDF
Intro to Advanced JavaScript
PDF
Your code sucks, let's fix it - PHP Master Series 2012
PDF
Your code sucks, let's fix it (CakeFest2012)
PDF
You code sucks, let's fix it
PDF
Nik Graf - Get started with Reason and ReasonReact
PDF
Your code sucks, let's fix it - DPC UnCon
Understanding Prototypal Inheritance
Jsphp 110312161301-phpapp02
Tactical DDD patterns in Go
Elegant objects
Object-oriented Javascript
ScotRuby - Dark side of ruby
JavaScript for PHP developers
A Few Interesting Things in Apple's Swift Programming Language
Your code sucks, let's fix it
Benefits of Kotlin
Your code sucks, let's fix it
Google Guava - Core libraries for Java & Android
Javascript ES6 generators
JavaScript - i och utanför webbläsaren (2010-03-03)
Intro to Advanced JavaScript
Your code sucks, let's fix it - PHP Master Series 2012
Your code sucks, let's fix it (CakeFest2012)
You code sucks, let's fix it
Nik Graf - Get started with Reason and ReasonReact
Your code sucks, let's fix it - DPC UnCon
Ad

More from crgwbr (6)

PDF
Lightning Talk: Making JS better with Browserify
PDF
07 Integration Project Part 1
PDF
03 Web Events and jQuery
PDF
02 Introduction to Javascript
PDF
01 Introduction To CSS
PDF
08 Integration Project Part 2
Lightning Talk: Making JS better with Browserify
07 Integration Project Part 1
03 Web Events and jQuery
02 Introduction to Javascript
01 Introduction To CSS
08 Integration Project Part 2
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Cloud computing and distributed systems.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
MYSQL Presentation for SQL database connectivity
PDF
KodekX | Application Modernization Development
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Cloud computing and distributed systems.
The Rise and Fall of 3GPP – Time for a Sabbatical?
“AI and Expert System Decision Support & Business Intelligence Systems”
sap open course for s4hana steps from ECC to s4
Dropbox Q2 2025 Financial Results & Investor Presentation
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
KodekX | Application Modernization Development
Understanding_Digital_Forensics_Presentation.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding

06 Map Reduce