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

PPTX
Functional Programming in JavaScript by Luis Atencio
PPT
Introduction to Functional Programming in JavaScript
PPTX
Functional programming in JavaScript
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
PDF
Functional Programming with JavaScript
PDF
Thinking in Functions: Functional Programming in Python
PDF
Functional Programming Patterns (BuildStuff '14)
PDF
Learning Functional Programming Without Growing a Neckbeard
Functional Programming in JavaScript by Luis Atencio
Introduction to Functional Programming in JavaScript
Functional programming in JavaScript
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Functional Programming with JavaScript
Thinking in Functions: Functional Programming in Python
Functional Programming Patterns (BuildStuff '14)
Learning Functional Programming Without Growing a Neckbeard

What's hot (20)

PPTX
An Introduction to Functional Programming with Javascript
PPTX
Category theory, Monads, and Duality in the world of (BIG) Data
PPTX
Functional Programming with JavaScript
PDF
Functional programming in Python
PDF
Reasoning about laziness
PDF
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
PDF
Functional Programming in JavaScript
PDF
Scala categorytheory
ODP
Clojure basics
ODP
Functional Programming With Scala
PDF
An introduction to property based testing
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
PDF
The lazy programmer's guide to writing thousands of tests
PDF
One Monad to Rule Them All
PDF
Functional programming ii
PDF
A taste of Functional Programming
PPTX
Functional programming
PDF
An Introduction to Functional Programming - DeveloperUG - 20140311
ODP
Knolx session
PDF
Function Composition - forward composition versus backward composition
An Introduction to Functional Programming with Javascript
Category theory, Monads, and Duality in the world of (BIG) Data
Functional Programming with JavaScript
Functional programming in Python
Reasoning about laziness
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Functional Programming in JavaScript
Scala categorytheory
Clojure basics
Functional Programming With Scala
An introduction to property based testing
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
The lazy programmer's guide to writing thousands of tests
One Monad to Rule Them All
Functional programming ii
A taste of Functional Programming
Functional programming
An Introduction to Functional Programming - DeveloperUG - 20140311
Knolx session
Function Composition - forward composition versus backward composition
Ad

Viewers also liked (20)

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

Similar to Intro to Functional Programming (20)

PPT
Savitch ch 04
PPT
Savitch Ch 04
PPT
Savitch Ch 04
PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
PPT
TDD And Refactoring
PDF
Java Lab Manual
PPTX
Functional and code coverage verification using System verilog
PDF
JavaProgrammingManual
PPTX
Function & Recursion
PDF
Effective Java with Groovy - How Language can Influence Good Practices
PDF
Twig tips and tricks
PPTX
Procedure and Functions in pl/sql
PPTX
Function
PDF
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
PDF
Object Oriented Programming using C++ - Part 4
PPTX
Data structures and algorithms lab2
PPTX
What’s new in .NET
PPT
Refactoring - improving the smell of your code
PPTX
Working effectively with legacy code
Savitch ch 04
Savitch Ch 04
Savitch Ch 04
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
TDD And Refactoring
Java Lab Manual
Functional and code coverage verification using System verilog
JavaProgrammingManual
Function & Recursion
Effective Java with Groovy - How Language can Influence Good Practices
Twig tips and tricks
Procedure and Functions in pl/sql
Function
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Object Oriented Programming using C++ - Part 4
Data structures and algorithms lab2
What’s new in .NET
Refactoring - improving the smell of your code
Working effectively with legacy code

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
AI in Product Development-omnex systems
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Nekopoi APK 2025 free lastest update
PPTX
history of c programming in notes for students .pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Choose the Right IT Partner for Your Business in Malaysia
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
AI in Product Development-omnex systems
Wondershare Filmora 15 Crack With Activation Key [2025
Odoo Companies in India – Driving Business Transformation.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Nekopoi APK 2025 free lastest update
history of c programming in notes for students .pptx
Odoo POS Development Services by CandidRoot Solutions
2025 Textile ERP Trends: SAP, Odoo & Oracle
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Reimagine Home Health with the Power of Agentic AI​
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...

Intro to Functional Programming