Intro to Functional Programming
-
Upload
hugo-firth -
Category
Software
-
view
96 -
download
0
Transcript of Intro to Functional Programming
![Page 1: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/1.jpg)
FUNCTIONAL PROGRAMMING Hugo Firth @hugo_firth
1
![Page 2: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/2.jpg)
WHAT I’LL BE TALKING ABOUT
2
▫︎Comparison of different paradigms
▫︎What is functional programming?
▫︎ Purity
▫︎ Laziness
▫︎Concurrency
![Page 3: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/3.jpg)
WHAT I WON’T BE TALKING ABOUT
3
▫︎ Functors
▫︎Monads
▫︎Category theory
▫︎ Type theory
![Page 4: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/4.jpg)
Clojure
![Page 5: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/5.jpg)
CLOJURE
5
▫︎ Lisp (funny syntax)
▫︎ Runs on the JVM
▫︎No classes, just functions
▫︎ Supports the functional paradigm
![Page 6: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/6.jpg)
DIFFERENT PARADIGMS
6
▫︎Machine code
▫︎ Procedural
▫︎Object Oriented
▫︎ Functional
▫︎Multi-paradigm
Many others such as logic and symbolic programming
![Page 7: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/7.jpg)
DIFFERENT PARADIGMS
7
▫︎Machine code
▫︎Procedural
▫︎Object Oriented
▫︎ Functional
▫︎Multi-paradigm
Many others such as logic and symbolic programming
![Page 8: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/8.jpg)
PROCEDURALBASIC and C are the most notable languages here
Gave us basic looping constructs and subroutines
Why?
To abstract away from GOTO statements
8
![Page 9: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/9.jpg)
OBJECT ORIENTED Many languages here Java, C#, C++,
etc.
Gave us Classes and Objects
Why?
To abstract away from global state
9
![Page 10: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/10.jpg)
FUNCTIONALIncreasingly large number of languages including Clojure, Scala and Haskell
Gave us Functions
Why?
Remove all state
10
![Page 11: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/11.jpg)
What is FP
![Page 12: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/12.jpg)
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
![Page 13: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/13.jpg)
PURITY
![Page 14: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/14.jpg)
PURITY?
14
Same arguments in == same result
No side effects!
Ever!
![Page 15: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/15.jpg)
WHY?
15
Easier to design, test and understand
Referential transparency
-> optimisations
-> laziness
![Page 16: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/16.jpg)
WHAT DOESN’T THIS LOOK LIKE
![Page 17: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/17.jpg)
IMPURE
17
function calculateTax(amount) { var taxRate = db.getTaxRate(); return taxRate / amount * 100;}
![Page 18: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/18.jpg)
IMPURE
18
function calculateTax(amount, taxRate) { db.saveTaxRate(taxRate); return taxRate / amount * 100;}
![Page 19: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/19.jpg)
PURE
19
function calculateTax(amount, taxRate) { return taxRate / amount * 100;}
![Page 20: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/20.jpg)
PURE
20
var tax = calculateTax(100, 10);// tax = 10
![Page 21: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/21.jpg)
WHAT DOESN’T THIS LOOK LIKE?
21
for (int i = 0; i < count; i++) { //do something}
![Page 22: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/22.jpg)
WHAT DOESN’T THIS LOOK LIKE?
22
while(someCondition) { //do something}
![Page 23: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/23.jpg)
WHAT DOESN’T THIS LOOK LIKE?
23
foreach(var something in somethings) { //do something}
![Page 24: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/24.jpg)
HOW DO YOU DO ANYTHING?
![Page 25: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/25.jpg)
HIGHER ORDER FUNCTIONS
A higher order function is one that takes one or more functions as input, or returns a
function.
25
![Page 26: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/26.jpg)
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]
![Page 27: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/27.jpg)
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]
![Page 28: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/28.jpg)
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
![Page 29: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/29.jpg)
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
![Page 30: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/30.jpg)
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
![Page 31: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/31.jpg)
HIGHER ORDER FUNCTIONS
31
Recursion and higher order functions are the two constructs that allow us to do
anything in a functional language
![Page 32: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/32.jpg)
DECLARATIVE PROGRAMMING
![Page 33: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/33.jpg)
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
![Page 34: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/34.jpg)
Clojure example
34
![Page 35: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/35.jpg)
WHY?
35
Readability - describe what you want! Not how to get it
![Page 36: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/36.jpg)
WHY?
36
Abstract away concepts of iteration, transformation, filtering, and accumulation.
Write functions to deal with elements not a sequence.
![Page 37: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/37.jpg)
WHY?
37
Maintainability - Small independently testable functions
![Page 38: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/38.jpg)
LAZINESS
![Page 39: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/39.jpg)
RANGE
Return infinite sequence of numbers
39
![Page 40: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/40.jpg)
REPEAT
Returns a lazy infinite sequence of supplied items
40
![Page 41: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/41.jpg)
CYCLE
Returns a lazy infinite sequence of items in a collection
41
![Page 42: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/42.jpg)
Clojure example
42
![Page 43: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/43.jpg)
WHY?
43
Code is not evaluated until you need it.
Code that doesn’t need to be evaluated won’t be.
![Page 44: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/44.jpg)
IMMUTABILITY
![Page 45: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/45.jpg)
IMMUTABLE CLASS
45
public class Dollars { private double value; public Dollars(double value) { this.value = value; }
public double getValue() { return this.value; }}
![Page 46: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/46.jpg)
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
![Page 47: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/47.jpg)
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); }}
![Page 48: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/48.jpg)
48
IMMUTABLE COLLECTIONS
(THEY’RE HARD)
![Page 49: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/49.jpg)
49
IMMUTABLE COLLECTIONS
Empty collection:
[ ]
//or
null
![Page 50: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/50.jpg)
50
IMMUTABLE COLLECTIONS
1 element collection:
var element = x;
//and
var restOfCollection =
EMPTY_COLLECTION;
![Page 51: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/51.jpg)
51
IMMUTABLE COLLECTIONS
2 element collection:
var element = y;
//and
var restOfCollection = collectionX;
![Page 52: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/52.jpg)
52
IMMUTABLE COLLECTIONS
3 element collection:
var element = z;
//and
var restOfCollection = collectionY;
![Page 53: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/53.jpg)
53
IMMUTABLE COLLECTIONS
IN PICTURES
![Page 54: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/54.jpg)
54
IMMUTABLE COLLECTIONS
EMPTY COLLECTION
[ ]
![Page 55: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/55.jpg)
55
IMMUTABLE COLLECTIONS
EMPTY COLLECTION X
[ X ]
![Page 56: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/56.jpg)
56
IMMUTABLE COLLECTIONS
EMPTY COLLECTION X Y
[ X, Y ]
![Page 57: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/57.jpg)
57
IMMUTABLE COLLECTIONS
EMPTY COLLECTION X Y Z
[ X, Y, Z ]
![Page 58: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/58.jpg)
58
IMMUTABLE COLLECTIONS
LOOKS FAMILIAR RIGHT?
![Page 59: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/59.jpg)
59
IMMUTABLE COLLECTIONS
EMPTY COLLECTION X Y Z
Z2
[ X, Y, Z ][ X, Y, Z2 ]
![Page 60: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/60.jpg)
Clojure example
60
![Page 61: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/61.jpg)
WHY?
61
Pass objects and collections to other functions with out fear of them changing
![Page 62: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/62.jpg)
CONCURRENCY
![Page 63: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/63.jpg)
Clojure example
63
![Page 64: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/64.jpg)
WHY?
64
Concurrency is hugely important with multi-core processors
Simplicity - data sharing across threads and processes is hard if it’s mutable
![Page 65: Intro to Functional Programming](https://reader033.fdocuments.us/reader033/viewer/2022051414/55a8ed0a1a28abb32b8b4735/html5/thumbnails/65.jpg)
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