Functional	Programming,
Is	It	Worth	It?
Andrew	Rollins,	Shift	Conference,	June	2016
Me:	Andrew	Rollins
• http://www.andrewrollins.com
• Twitter:	@andrew311
• Boston,	USA
• Co-founder	of	Localytics
• Long	time	functional	programmer
• Now	at	Sigma	Prime	Ventures	(Boston	VC)
Andrew	Rollins,	Split,	June	2016
Have	you	encountered…
monad
functor
applicative
and	said	“WTF?”
Andrew	Rollins,	Split,	June	2016
You	are	not	alone.
Andrew	Rollins,	Split,	June	2016
Andrew	Rollins,	Split,	June	2016
Learning	FP	is	like	Officer	
Barbrady learning	to	read	
(second	season,	fourth	episode	of	South	Park)
Andrew	Rollins,	Split,	June	2016
Officer	Barbrady confronts	his	
illiteracy,	learning	to	read	so	he	
can	solve	a	crime!
Upon	solving	the	crime,	he’s	
given	a	copy	of	this:
Andrew	Rollins,	Split,	June	2016
Leading	to	a	memorable	South	
Park	quote…
Andrew	Rollins,	Split,	June	2016
Andrew	Rollins,	Split,	June	2016
Yes,	at	first	I	was	happy	to	be	
learning	how	to	read.
It	seemed	exciting	and	
magical,	but	then	I	read	this:	
Atlas	Shrugged by	Ayn	Rand.
I	read	every	last	word	of	this	
garbage,	and	because	of	this	
piece	of	s**t,	I	am	never	
reading	again.
Andrew	Rollins,	Split,	June	2016
Officer	Barbrady’s experience	
sums	up	many	people’s	
experience	with	Functional	
Programming.
Andrew	Rollins,	Split,	June	2016
Exciting	at	first…
But	then	you	ecounter	crazy	
s**t,	and	you	have	an	Officer	
Barbrady	moment.
Andrew	Rollins,	Split,	June	2016
Crazy	stuff	like…
•monads
•functors
•monoids
•applicatives
•category	theory
•blah	blah	blah
Andrew	Rollins,	Split,	June	2016
I	saw	this	happen	across	
engineering	at	Localytics
Andrew	Rollins,	Split,	June	2016
Does	it	have	to	be	this	way?
Is	FP	worth	it?
Andrew	Rollins,	Split,	June	2016
I’m	here	to	say…
Yes,	FP	is	worth	it!
We	can’t	get	hung	up	on	this.
Andrew	Rollins,	Split,	June	2016
Look	past	the…
•monads
•functors
•monoids
•applicatives
•category	theory
•blah	blah	blah
And	find	the	useful	things…
Andrew	Rollins,	Split,	June	2016
•pure	functions
•higher	order	functions
•immutable	data
•isolating	side	effects
•using	types	effectively
•and	so	on
Andrew	Rollins,	Split,	June	2016
My	goal	for	the	rest	of	this	
presentation	is	to	help	you	
overcome	your	Officer	Barbrady
moment	and	find	a	path	to	
success	in	FP.
Let’s	get	to	it!
Andrew	Rollins,	Split,	June	2016
Tip
FP	is	about	a	core	set	of	principles
Andrew	Rollins,	Split,	June	2016
Principles
•Pure	functions
•Higher	order	functions
•Immutable	data
•Isolation	of	side	effects
Andrew	Rollins,	Split,	June	2016
Pure	functions
•Purity	is	all	about	making	functions	self-contained	and	
repeatable
•foo(x)	should	NOT
• read	or	manipulate	state	outside	the	function
• change	the	caller’s	version	of	x
•foo(x)	should
• return	the	same	results	for	the	same	argument,	always
Andrew	Rollins,	Split,	June	2016
Why	pure	functions?
•Because	purity	makes	a	function	easier	to	reason	
about	and	test
•Purity	guarantees	that	you	aren’t	dependent	on	state	
changes	outside	of	the	function
•Purity	means	that	your	tests	can	be	concerned	with	
simple	input	and	output	checks
Andrew	Rollins,	Split,	June	2016
Higher	order	functions
•HoF is	about	being	able	to	pass	around	functions	as	
values
Andrew	Rollins,	Split,	June	2016
Why	higher	order	functions?
•Because	they	let	us	write	useful	methods	like	map
•Most	people	use	languages	with	HoF these	days,	so	
this	shouldn’t	require	much	convincing
Andrew	Rollins,	Split,	June	2016
Immutable	data
•Immutability	is	about	locking	down	objects	once	they	
are	created
•An	object	should	NOT	update	its	internal	state	via	
object	methods
•State	changes	should	be	reflected	in	new	objects
•A	programmer	should	not	change	variables	once	
created
Andrew	Rollins,	Split,	June	2016
Why	immutable	data?
•Because	immutability	makes	it	easier	to	reason	about	
state	in	your	program
•Variable	x	is	always	the	same	x
•Threads	couldn’t	possibly	stomp	on	each	others’	state	
if	the	state	is	immutable
Andrew	Rollins,	Split,	June	2016
Isolation	of	side	effects
•Isolation	of	side	effects	is	about	being	disciplined	with	
regards	to	side	effects	and	where	they	happen
•We	recognize	that	code	must	have	side	effects	to	do	
anything	useful,	this	is	about	techniques	which	let	us	
minimize	the	surface	area	of	side	effects
Andrew	Rollins,	Split,	June	2016
Why	isolation	of	side	effects?
•Because	functions	without	side	effects	are	easier	to	
test	and	reason	about
•If	we	isolate	side	effects	to	a	smaller	portion	of	code,	
testing	is	easier
Andrew	Rollins,	Split,	June	2016
Read	this	for	more	about	principles:
“Intro	To	Functional	Programming”
by	Marko	Pavlovic
http://markonis.github.io/functional/programming/2016/04/
16/intro-to-functional-programming.html
Andrew	Rollins,	Split,	June	2016
Tip
When	learning	FP,	it’s	important	to	
separate	the	principles	from	the	
tools
Andrew	Rollins,	Split,	June	2016
Principle
Measure	twice,	cut	once
Tool
saw
Andrew	Rollins,	Split,	June	2016
Principle
Don’t	use	null
Tool
Option
Andrew	Rollins,	Split,	June	2016
Tools
• For	comprehensions
• Pattern	matching
• Functors
• Monads
• Monoids
• Applicatives
• Either
• Option
Andrew	Rollins,	Split,	June	2016
Law	of	the	Instrument
"Give	a	small	boy	a	hammer,	and	he	
will	find	that	everything	he	
encounters	needs	pounding.”
- Abraham	Kaplan,	in	1964
Andrew	Rollins,	Split,	June	2016
Law	of	the	Instrument
“If	all	you	have	is	a	hammer,	
everything	looks	like	a	nail.”
- Abraham	Maslow,	in	1966
Andrew	Rollins,	Split,	June	2016
Beware	of	this	effect	with…
• Functors
• Monads
• Monoids
• Applicatives
• Either
• Option
Andrew	Rollins,	Split,	June	2016
hammer	=	monad
Andrew	Rollins,	Split,	June	2016
Andrew’s	Corollary	to	Law	of	the	Instrument
“If	all	you	have	is	a	hammer,	and	someone	
asks	you	to	build	a	house,	you’re	gonna have	
a	bad	time.”
- Andrew	Rollins	(me),	in	2016
Andrew	Rollins,	Split,	June	2016
It	is	exceptionally	hard	to	build	a	
house	(application)	when	you	only	
have	a	hammer	(monad)
Andrew	Rollins,	Split,	June	2016
Treat	FP	tools	like	any	other	tools	in	your	
toolbox,	and	realize	that	it	takes	a	set	of	tools	
to	build	a	house.
Andrew	Rollins,	Split,	June	2016
Tip
I	like	to	think	of	functors,	monads,	
etc.	as	a	special	kind	of	container.
They	are	just	codified	patterns.
Andrew	Rollins,	Split,	June	2016
There	are	many	kinds	of	containers:
linked	list,	hash,	array,	tree,	queue
But	they	usually	have	common	
interfaces:
Java	Collection,	Ruby	Enumerable
Andrew	Rollins,	Split,	June	2016
Quoting	Russ	Bishop:
Functors are	containers	you	can	
call map on
Monads	are	containers	you	can	
call flatMap on	
“Functors,	Applicatives,	and	Monads	in	Plain	English”
http://www.russbishop.net/monoids-monads-and-functors
Andrew	Rollins,	Split,	June	2016
It’s	a	simplification,	but	it’ll	get	you	
started.
Andrew	Rollins,	Split,	June	2016
Also	check	out:
“Functors,	Applicatives,	And	Monads	
in	Pictures”
by	Aditya	Bhargava
http://adit.io/posts/2013-04-17-
functors,_applicatives,_and_monads_in_pictures.html
Andrew	Rollins,	Split,	June	2016
Tip
Use	libraries,	but	beware.
Andrew	Rollins,	Split,	June	2016
It	helps	to	read	about	“Typeclasses”,	
especially	for	Haskell	and	Scala
“Learn	You	a	Haskell,	Types	and	Typeclasses”
http://learnyouahaskell.com/types-and-typeclasses
Andrew	Rollins,	Split,	June	2016
Many	libraries	use	Typeclasses as	an	
alternative	to	typical	object	
inheritance	and	interfaces.
It	takes	time	to	become	comfortable	
with	this	pattern.
Andrew	Rollins,	Split,	June	2016
Wrapping	up
You	already	FP
Andrew	Rollins,	Split,	June	2016
Any	time	you…
• Create	a	self-contained	function	without	side	effects
• Pass	around	functions	as	input	/	output	to	other	functions
• Create	immutable	objects	instead	of	mutating	object	state
• Use	return	values	instead	of	exceptions
Andrew	Rollins,	Split,	June	2016
Andrew	Rollins,	Split,	June	2016
Then	why	does	the	Barbrady
moment	happen?
Because	FP	wanders	into	some	
hard	topics.
“A	monad	is	just	a	monoid	in	the	category	of	
endofunctors,	what's	the	problem?”
- James	Iry
Andrew	Rollins,	Split,	June	2016
Andrew	Rollins,	Split,	June	2016
Don’t	let	it	get	you	down.
FP	is	just	a	style	of	
programming.
It	might	help	you	write	better	
code.
Don’t	sweat	the	small	stuff.
Takeaways
•first	focus	on	the	principles
•read	those	links
•mess	around	with	tools
•don’t	get	bogged	down	by	tools
•try	some	popular	libraries
Andrew	Rollins,	Split,	June	2016
Andrew	Rollins,	Split,	June	2016
• Andrew	Rollins
• Twitter:	@andrew311
• http://andrewrollins.com
Happy	programming!
Thank	you!

Functional Programming, Is It Worth It?