KEMBAR78
Intro to Functional Programming | PDF
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

Intro to Functional Programming