intro to functional programming

65
FUNCTIONAL PROGRAMMING Hugo Firth @hugo_rth 1

Upload: hugo-firth

Post on 17-Jul-2015

96 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Intro to Functional Programming

FUNCTIONAL PROGRAMMING Hugo Firth @hugo_firth

1

Page 2: Intro to Functional Programming

WHAT I’LL BE TALKING ABOUT

2

▫︎Comparison of different paradigms

▫︎What is functional programming?

▫︎ Purity

▫︎ Laziness

▫︎Concurrency

Page 3: Intro to Functional Programming

WHAT I WON’T BE TALKING ABOUT

3

▫︎ Functors

▫︎Monads

▫︎Category theory

▫︎ Type theory

Page 4: Intro to Functional Programming

Clojure

Page 5: Intro to Functional Programming

CLOJURE

5

▫︎ Lisp (funny syntax)

▫︎ Runs on the JVM

▫︎No classes, just functions

▫︎ Supports the functional paradigm

Page 6: Intro to Functional Programming

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

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

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

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

FUNCTIONALIncreasingly large number of languages including Clojure, Scala and Haskell

Gave us Functions

Why?

Remove all state

10

Page 11: Intro to Functional Programming

What is FP

Page 12: Intro to Functional Programming

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

PURITY

Page 14: Intro to Functional Programming

PURITY?

14

Same arguments in == same result

No side effects!

Ever!

Page 15: Intro to Functional Programming

WHY?

15

Easier to design, test and understand

Referential transparency

-> optimisations

-> laziness

Page 16: Intro to Functional Programming

WHAT DOESN’T THIS LOOK LIKE

Page 17: Intro to Functional Programming

IMPURE

17

function calculateTax(amount) { var taxRate = db.getTaxRate(); return taxRate / amount * 100;}

Page 18: Intro to Functional Programming

IMPURE

18

function calculateTax(amount, taxRate) { db.saveTaxRate(taxRate); return taxRate / amount * 100;}

Page 19: Intro to Functional Programming

PURE

19

function calculateTax(amount, taxRate) { return taxRate / amount * 100;}

Page 20: Intro to Functional Programming

PURE

20

var tax = calculateTax(100, 10);// tax = 10

Page 21: Intro to Functional Programming

WHAT DOESN’T THIS LOOK LIKE?

21

for (int i = 0; i < count; i++) { //do something}

Page 22: Intro to Functional Programming

WHAT DOESN’T THIS LOOK LIKE?

22

while(someCondition) { //do something}

Page 23: Intro to Functional Programming

WHAT DOESN’T THIS LOOK LIKE?

23

foreach(var something in somethings) { //do something}

Page 24: Intro to Functional Programming

HOW DO YOU DO ANYTHING?

Page 25: Intro to Functional Programming

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

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

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

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

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

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

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

DECLARATIVE PROGRAMMING

Page 33: Intro to Functional 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

Page 34: Intro to Functional Programming

Clojure example

34

Page 35: Intro to Functional Programming

WHY?

35

Readability - describe what you want! Not how to get it

Page 36: Intro to Functional Programming

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

WHY?

37

Maintainability - Small independently testable functions

Page 38: Intro to Functional Programming

LAZINESS

Page 39: Intro to Functional Programming

RANGE

Return infinite sequence of numbers

39

Page 40: Intro to Functional Programming

REPEAT

Returns a lazy infinite sequence of supplied items

40

Page 41: Intro to Functional Programming

CYCLE

Returns a lazy infinite sequence of items in a collection

41

Page 42: Intro to Functional Programming

Clojure example

42

Page 43: Intro to Functional Programming

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

IMMUTABILITY

Page 45: Intro to Functional Programming

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

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

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

48

IMMUTABLE COLLECTIONS

(THEY’RE HARD)

Page 49: Intro to Functional Programming

49

IMMUTABLE COLLECTIONS

Empty collection:

[ ]

//or

null

Page 50: Intro to Functional Programming

50

IMMUTABLE COLLECTIONS

1 element collection:

var element = x;

//and

var restOfCollection =

EMPTY_COLLECTION;

Page 51: Intro to Functional Programming

51

IMMUTABLE COLLECTIONS

2 element collection:

var element = y;

//and

var restOfCollection = collectionX;

Page 52: Intro to Functional Programming

52

IMMUTABLE COLLECTIONS

3 element collection:

var element = z;

//and

var restOfCollection = collectionY;

Page 53: Intro to Functional Programming

53

IMMUTABLE COLLECTIONS

IN PICTURES

Page 54: Intro to Functional Programming

54

IMMUTABLE COLLECTIONS

EMPTY COLLECTION

[ ]

Page 55: Intro to Functional Programming

55

IMMUTABLE COLLECTIONS

EMPTY COLLECTION X

[ X ]

Page 56: Intro to Functional Programming

56

IMMUTABLE COLLECTIONS

EMPTY COLLECTION X Y

[ X, Y ]

Page 57: Intro to Functional Programming

57

IMMUTABLE COLLECTIONS

EMPTY COLLECTION X Y Z

[ X, Y, Z ]

Page 58: Intro to Functional Programming

58

IMMUTABLE COLLECTIONS

LOOKS FAMILIAR RIGHT?

Page 59: Intro to Functional Programming

59

IMMUTABLE COLLECTIONS

EMPTY COLLECTION X Y Z

Z2

[ X, Y, Z ][ X, Y, Z2 ]

Page 60: Intro to Functional Programming

Clojure example

60

Page 61: Intro to Functional Programming

WHY?

61

Pass objects and collections to other functions with out fear of them changing

Page 62: Intro to Functional Programming

CONCURRENCY

Page 63: Intro to Functional Programming

Clojure example

63

Page 64: Intro to Functional Programming

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

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