SlideShare a Scribd company logo
FUNCTIONAL
PROGRAMMING
Hugo Firth
@hugo_firth
1
WHAT I’LL BE TALKING ABOUT
2
▫︎Comparison of different paradigms
▫︎What is functional programming?
▫︎Purity
▫︎Laziness
▫︎Concurrency
WHAT I WON’T BE TALKING ABOUT
3
▫︎Functors
▫︎Monads
▫︎Category theory
▫︎Type theory
Clojure
CLOJURE
5
▫︎Lisp (funny syntax)
▫︎Runs on the JVM
▫︎No classes, just functions
▫︎Supports the functional paradigm
DIFFERENT PARADIGMS
6
▫︎Machine code
▫︎Procedural
▫︎Object Oriented
▫︎Functional
▫︎Multi-paradigm
Many others such as logic and symbolic
programming
DIFFERENT PARADIGMS
7
▫︎Machine code
▫︎Procedural
▫︎Object Oriented
▫︎Functional
▫︎Multi-paradigm
Many others such as logic and symbolic
programming
PROCEDURAL
BASIC and C are the most notable
languages here
Gave us basic looping constructs and
subroutines
Why?
To abstract away from GOTO
statements
8
OBJECT
ORIENTED
Many languages here Java, C#, C++,
etc.


Gave us Classes and Objects


Why?
To abstract away from global state
9
FUNCTIONAL
Increasingly large number of
languages including Clojure, Scala and
Haskell
Gave us Functions


Why?
Remove all state
10
What is FP
DEFINITION
A style of building the structure and
elements of computer programs, that treats
computation as the evaluation of
mathematical functions and avoids state
and mutable data
12http://en.wikipedia.org/wiki/Functional_programming
PURITY
PURITY?
14
Same arguments in == same result
No side effects!
Ever!
WHY?
15
Easier to design, test and understand
Referential transparency 

-> optimisations 

-> laziness
WHAT DOESN’T
THIS LOOK LIKE
IMPURE
17
function calculateTax(amount) {
var taxRate = db.getTaxRate();
return taxRate / amount * 100;
}
IMPURE
18
function calculateTax(amount, taxRate) {
db.saveTaxRate(taxRate);
return taxRate / amount * 100;
}
PURE
19
function calculateTax(amount, taxRate) {
return taxRate / amount * 100;
}
PURE
20
var tax = calculateTax(100, 10);
// tax = 10
WHAT DOESN’T THIS LOOK LIKE?
21
for (int i = 0; i < count; i++) {
//do something
}
WHAT DOESN’T THIS LOOK LIKE?
22
while(someCondition) {
//do something
}
WHAT DOESN’T THIS LOOK LIKE?
23
foreach(var something in somethings) {
//do something
}
HOW DO YOU DO
ANYTHING?
HIGHER ORDER FUNCTIONS
A higher order function is one that takes one
or more functions as input, or returns a
function.
25
IMPURE
26
var numbers = [1, 2, 3, 4, 5];
for(var i = 0; i < numbers.length; i++) {
numbers[i] = numbers[i] * 2;
}
//numbers => [2, 4, 6, 8, 10]
IMPURE
27
var numbers = [1, 2, 3, 4, 5];
var destination = [];
for(var i = 0; i < numbers.length; i++) {
var result = numbers[i] * 2;
destination.push(result);
}
//destination => [2, 4, 6, 8, 10]
MAP (HIGHER ORDER FUNCTION)
Map is the name of a higher-order function
that applies a given function to each
element of a list, returning a list of results
28
FILTER (HIGHER ORDER FUNCTION)
Filter is a higher-order function that
processes a collection to produce a new
collection containing exactly those elements
of the original collection for which a given
predicate returns true
29
FOLD / REDUCE / AGGREGATE
A family of higher-order functions that
analyse a recursive data structure and
recombine through use of a given combining
operation the results of recursively
processing its constituent parts, building up
a return value
30
HIGHER ORDER FUNCTIONS
31
Recursion and higher order functions are
the two constructs that allow us to do
anything in a functional language
DECLARATIVE
PROGRAMMING
WHAT IS THIS DOING!?!
33
var n = 1;
var num_elements = 0;
var sum_of_first_10 = 0;
while (num_elements < 10) {
if (n^2 % 5 == 0) {
sum_of_first_10 += n;
num_elements += 1;
}
n += 1;
}
//sum_of_first_10 => 225
Clojure example
34
WHY?
35
Readability - describe what you want! Not
how to get it
WHY?
36
Abstract away concepts of iteration,
transformation, filtering, and accumulation.
Write functions to deal with elements not a
sequence.
WHY?
37
Maintainability - Small independently
testable functions
LAZINESS
RANGE
Return infinite sequence of numbers
39
REPEAT
Returns a lazy infinite sequence of supplied
items
40
CYCLE
Returns a lazy infinite sequence of items in a
collection
41
Clojure example
42
WHY?
43
Code is not evaluated until you need it.
Code that doesn’t need to be evaluated
won’t be.
IMMUTABILITY
IMMUTABLE CLASS
45
public class Dollars {
private double value;
public Dollars(double value) {
this.value = value;
}
public double getValue() {
return this.value;
}
}
46
public class Dollars {
private double value;
public Dollars(double value) {
this.value = value;
}
public double getValue() {
return this.value;
}
public void add(double value) {
this.value += value;
}
}
MUTABLE CLASS
47
IMMUTABLE CLASS
public class Dollars {
private double value;
public Dollars(double value) {
this.value = value;
}
public double getValue() {
return this.value;
}
public Dollars add(double value) {
return new
Dollars(this.value + value);
}
}
48
IMMUTABLE COLLECTIONS
(THEY’RE HARD)
49
IMMUTABLE COLLECTIONS
Empty collection:
[ ]
//or
null
50
IMMUTABLE COLLECTIONS
1 element collection:
var element = x;
//and
var restOfCollection =
EMPTY_COLLECTION;
51
IMMUTABLE COLLECTIONS
2 element collection:
var element = y;
//and
var restOfCollection =
collectionX;
52
IMMUTABLE COLLECTIONS
3 element collection:
var element = z;
//and
var restOfCollection =
collectionY;
53
IMMUTABLE COLLECTIONS
IN PICTURES
54
IMMUTABLE COLLECTIONS
EMPTY
COLLECTION
[ ]
55
IMMUTABLE COLLECTIONS
EMPTY
COLLECTION
X
[ X ]
56
IMMUTABLE COLLECTIONS
EMPTY
COLLECTION
X Y
[ X, Y ]
57
IMMUTABLE COLLECTIONS
EMPTY
COLLECTION
X Y Z
[ X, Y, Z ]
58
IMMUTABLE COLLECTIONS
LOOKS FAMILIAR RIGHT?
59
IMMUTABLE COLLECTIONS
EMPTY
COLLECTION
X Y Z
Z2
[ X, Y, Z ]
[ X, Y, Z2 ]
Clojure example
60
WHY?
61
Pass objects and collections to other
functions with out fear of them changing
CONCURRENCY
Clojure example
63
WHY?
64
Concurrency is hugely important with multi-
core processors
Simplicity - data sharing across threads and
processes is hard if it’s mutable
YOU CAN DO THIS IN ANY LANGUAGE!!!!
▫︎Try to write pure small pure methods/functions
▫︎Avoid state wherever you can
▫︎Separate impurities when they can’t be removed
▫︎Create immutable classes (value objects)
▫︎Look at simpler methods for concurrency
65

More Related Content

What's hot (20)

PPTX
An Introduction to Functional Programming with Javascript
Doug Sparling
 
PPTX
Category theory, Monads, and Duality in the world of (BIG) Data
greenwop
 
PPTX
Functional Programming with JavaScript
Aung Baw
 
PDF
Functional programming in Python
Colin Su
 
PDF
Reasoning about laziness
Johan Tibell
 
PDF
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
John De Goes
 
PDF
Functional Programming in JavaScript
Will Livengood
 
PDF
Scala categorytheory
Knoldus Inc.
 
ODP
Clojure basics
Knoldus Inc.
 
ODP
Functional Programming With Scala
Knoldus Inc.
 
PDF
An introduction to property based testing
Scott Wlaschin
 
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Philip Schwarz
 
PDF
The lazy programmer's guide to writing thousands of tests
Scott Wlaschin
 
PDF
One Monad to Rule Them All
John De Goes
 
PDF
Functional programming ii
Prashant Kalkar
 
PDF
A taste of Functional Programming
Jordan Open Source Association
 
PPTX
Functional programming
Prashant Kalkar
 
PDF
An Introduction to Functional Programming - DeveloperUG - 20140311
Andreas Pauley
 
ODP
Knolx session
Knoldus Inc.
 
PDF
Function Composition - forward composition versus backward composition
Philip Schwarz
 
An Introduction to Functional Programming with Javascript
Doug Sparling
 
Category theory, Monads, and Duality in the world of (BIG) Data
greenwop
 
Functional Programming with JavaScript
Aung Baw
 
Functional programming in Python
Colin Su
 
Reasoning about laziness
Johan Tibell
 
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
John De Goes
 
Functional Programming in JavaScript
Will Livengood
 
Scala categorytheory
Knoldus Inc.
 
Clojure basics
Knoldus Inc.
 
Functional Programming With Scala
Knoldus Inc.
 
An introduction to property based testing
Scott Wlaschin
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Philip Schwarz
 
The lazy programmer's guide to writing thousands of tests
Scott Wlaschin
 
One Monad to Rule Them All
John De Goes
 
Functional programming ii
Prashant Kalkar
 
A taste of Functional Programming
Jordan Open Source Association
 
Functional programming
Prashant Kalkar
 
An Introduction to Functional Programming - DeveloperUG - 20140311
Andreas Pauley
 
Knolx session
Knoldus Inc.
 
Function Composition - forward composition versus backward composition
Philip Schwarz
 

Viewers also liked (20)

PDF
Анонимные записи в Haskell. Никита Волков
Юрий Сыровецкий
 
PDF
Монады для барабанщиков. Антон Холомьёв
Юрий Сыровецкий
 
PDF
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Andrey Breslav
 
PDF
Airbnb tech talk: Levi Weintraub on webkit
naseemh
 
PDF
Pushing Python: Building a High Throughput, Low Latency System
Kevin Ballard
 
PDF
London React August - GraphQL at The Financial Times - Viktor Charypar
React London Community
 
PDF
CSS/SVG Matrix Transforms
Marc Grabanski
 
PDF
HTML5 Essentials
Marc Grabanski
 
PDF
jQuery Essentials
Marc Grabanski
 
PDF
Domain Modeling in a Functional World
Debasish Ghosh
 
PDF
gevent at TellApart
TellApart
 
PDF
Map, Flatmap and Reduce are Your New Best Friends: Simpler Collections, Concu...
Chris Richardson
 
PPTX
Category theory for beginners
kenbot
 
PPTX
Introduction to Storm
Chandler Huang
 
PPTX
DNS Security Presentation ISSA
Srikrupa Srivatsan
 
PDF
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
mumrah
 
PPTX
From cache to in-memory data grid. Introduction to Hazelcast.
Taras Matyashovsky
 
PDF
Cassandra Introduction & Features
DataStax Academy
 
PDF
Etsy Activity Feeds Architecture
Dan McKinley
 
PPTX
Introduction to Apache ZooKeeper
Saurav Haloi
 
Анонимные записи в Haskell. Никита Волков
Юрий Сыровецкий
 
Монады для барабанщиков. Антон Холомьёв
Юрий Сыровецкий
 
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Andrey Breslav
 
Airbnb tech talk: Levi Weintraub on webkit
naseemh
 
Pushing Python: Building a High Throughput, Low Latency System
Kevin Ballard
 
London React August - GraphQL at The Financial Times - Viktor Charypar
React London Community
 
CSS/SVG Matrix Transforms
Marc Grabanski
 
HTML5 Essentials
Marc Grabanski
 
jQuery Essentials
Marc Grabanski
 
Domain Modeling in a Functional World
Debasish Ghosh
 
gevent at TellApart
TellApart
 
Map, Flatmap and Reduce are Your New Best Friends: Simpler Collections, Concu...
Chris Richardson
 
Category theory for beginners
kenbot
 
Introduction to Storm
Chandler Huang
 
DNS Security Presentation ISSA
Srikrupa Srivatsan
 
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
mumrah
 
From cache to in-memory data grid. Introduction to Hazelcast.
Taras Matyashovsky
 
Cassandra Introduction & Features
DataStax Academy
 
Etsy Activity Feeds Architecture
Dan McKinley
 
Introduction to Apache ZooKeeper
Saurav Haloi
 
Ad

Similar to Intro to Functional Programming (20)

PDF
Functional programming
Hideshi Ogoshi
 
PPTX
The joy of functional programming
Steve Zhang
 
PDF
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
PPTX
About Functional Programming
Aapo Kyrölä
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
PPT
Functional Programming Past Present Future
IndicThreads
 
PPT
Clojure 1a
Krishna Chaytaniah
 
KEY
Exciting JavaScript - Part II
Eugene Lazutkin
 
PPTX
Introduction to Functional Programming and Clojure
Soumendra Daas
 
PPTX
Good functional programming is good programming
kenbot
 
PDF
Thinking Functionally
Piyush Katariya
 
PDF
379008-rc217-functionalprogramming
Luis Atencio
 
PDF
Functional programming with clojure
Lucy Fang
 
PDF
Functional programming is the most extreme programming
samthemonad
 
PDF
Functional programming techniques in regular JavaScript
Pavel Klimiankou
 
PDF
Thinking Functionally - John Stevenson - Codemotion Rome 2017
Codemotion
 
PDF
Thinking Functionally with Clojure
John Stevenson
 
PDF
OOP and FP
Mario Fusco
 
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
PPTX
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Functional programming
Hideshi Ogoshi
 
The joy of functional programming
Steve Zhang
 
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
About Functional Programming
Aapo Kyrölä
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional Programming Past Present Future
IndicThreads
 
Exciting JavaScript - Part II
Eugene Lazutkin
 
Introduction to Functional Programming and Clojure
Soumendra Daas
 
Good functional programming is good programming
kenbot
 
Thinking Functionally
Piyush Katariya
 
379008-rc217-functionalprogramming
Luis Atencio
 
Functional programming with clojure
Lucy Fang
 
Functional programming is the most extreme programming
samthemonad
 
Functional programming techniques in regular JavaScript
Pavel Klimiankou
 
Thinking Functionally - John Stevenson - Codemotion Rome 2017
Codemotion
 
Thinking Functionally with Clojure
John Stevenson
 
OOP and FP
Mario Fusco
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Ad

Recently uploaded (20)

PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 

Intro to Functional Programming