Slide 1

Slide 1 text

Dysfunctional Programming

Slide 2

Slide 2 text

@igorwesome

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Immuteability: The property of functional programmers that prevents them from shutting up about pure functional programming. – Reg Braithwaite @raganwald

Slide 7

Slide 7 text

dys·func·tion·al

Slide 8

Slide 8 text

php λ

Slide 9

Slide 9 text

Paradigms

Slide 10

Slide 10 text

procedural Object- Oriented functional logic

Slide 11

Slide 11 text

Imperative Declarative

Slide 12

Slide 12 text

Structure Time & State

Slide 13

Slide 13 text

Structure

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

‘sup?

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

it’s not about the money

Slide 18

Slide 18 text

• Message passing • Interfaces • Composition • (Not inheritance)

Slide 19

Slide 19 text

Actor model: Asynchronous

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

f g h

Slide 23

Slide 23 text

tools and materials

Slide 24

Slide 24 text

• Value semantics • Higher-order functions • Lazy evaluation

Slide 25

Slide 25 text

The Garbage Overfloweth

Slide 26

Slide 26 text

$numberCollection = new NumberCollection(); $numberParser = new SimpleNumberStringParser(); $firstParsedNumber = $numberParser->parse('1'); $firstNumber = new SimpleNumber($firstParsedNumber); $firstNumberProxy = new CollectionItemNumberProxy($firstNumber); $numberCollection->add($firstNumberProxy); $secondParsedNumber = $numberParser->parse('1'); $secondNumber = new SimpleNumber($secondParsedNumber); $secondNumberProxy = new CollectionItemNumberProxy($secondNumber); $numberCollection->add($secondNumberProxy); $addition = new AdditionOperator('SimplePHPEasyPlus\Number\SimpleNumber'); $operation = new ArithmeticOperation($addition); $engine = new Engine($operation); $calcul = new Calcul($engine, $numberCollection); $runner = new CalculRunner(); $runner->run($calcul); $result = $calcul->getResult(); $numericResult = $result->getValue();

Slide 27

Slide 27 text

1 + 1

Slide 28

Slide 28 text

Composition

Slide 29

Slide 29 text

c a b a b

Slide 30

Slide 30 text

a b c

Slide 31

Slide 31 text

Higher-order functions

Slide 32

Slide 32 text

Dependency Injection

Slide 33

Slide 33 text

Inversion of control is really just a pretentious way of saying “Taking an argument”. – Rúnar Óli @runarorama

Slide 34

Slide 34 text

Taking an argument

Slide 35

Slide 35 text

g g’

Slide 36

Slide 36 text

0 1 2 3 4 even? 0 2 4

Slide 37

Slide 37 text

0 1 2 3 4 #(+ 5 %) 5 7 9 6 8

Slide 38

Slide 38 text

3 6 7 9 + 0 9 25 3 16 acumulator

Slide 39

Slide 39 text

Datastructures

Slide 40

Slide 40 text

Number 42 String "Hello, cruel world." Map {:foo "bar", :baz "qux"} List [:a :b :c] Set #{"Arthur Dent" "Ford Prefect"}

Slide 41

Slide 41 text

function is a means of abstraction

Slide 42

Slide 42 text

data is a means of abstraction

Slide 43

Slide 43 text

Structure Time & State

Slide 44

Slide 44 text

Time & State

Slide 45

Slide 45 text

Place-Oriented Programming

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

identity

Slide 51

Slide 51 text

new Foo("x") !== new Foo("x")

Slide 52

Slide 52 text

defensive copy

Slide 53

Slide 53 text

$users = new UserList(); $users->add(new User("Igor")); notify($users);

Slide 54

Slide 54 text

while (count($users)) { $user = $users->shift(); notifyUser($user); }

Slide 55

Slide 55 text

notify(clone $users)

Slide 56

Slide 56 text

observation

Slide 57

Slide 57 text

$user->getName()

Slide 58

Slide 58 text

$user["name"]

Slide 59

Slide 59 text

Value

Slide 60

Slide 60 text

Primitives

Slide 61

Slide 61 text

Value Object

Slide 62

Slide 62 text

date address money response request

Slide 63

Slide 63 text

serialization

Slide 64

Slide 64 text

History

Slide 65

Slide 65 text

Objects are forgetful

Slide 66

Slide 66 text

$sophie->setName("Joanna")

Slide 67

Slide 67 text

2013-09-13 01:01:47 Sophie 2013-09-13 01:02:25 Joanna 2013-09-13 01:05:31 Caroline

Slide 68

Slide 68 text

$joanna = $sophie->setName("Joanna")

Slide 69

Slide 69 text

Git

Slide 70

Slide 70 text

98ca9 34ac2 f30ab master Snapshot A Snapshot B Snapshot C

Slide 71

Slide 71 text

Epochal Time Model

Slide 72

Slide 72 text

v1 v2 v3 v4 f f f identity states events

Slide 73

Slide 73 text

Lazy evaluation

Slide 74

Slide 74 text

Iterator Generator Stream

Slide 75

Slide 75 text

a b c d a’ b’ c’ d’ consume

Slide 76

Slide 76 text

Order of execution becomes implicit

Slide 77

Slide 77 text

๏ filter ๏ map ๏ reduce

Slide 78

Slide 78 text

What about I/O?

Slide 79

Slide 79 text

Filesystem Database Network }is a place

Slide 80

Slide 80 text

Separate I/O from computation

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

What about testing?

Slide 83

Slide 83 text

But I heard static methods are untestable and evil

Slide 84

Slide 84 text

You cannot mock functions

Slide 85

Slide 85 text

Why do we mock?

Slide 86

Slide 86 text

Functions are fast

Slide 87

Slide 87 text

Impure parts are mocked by passing functions

Slide 88

Slide 88 text

my_beautiful_program(function () { return do_evil_shit(); })

Slide 89

Slide 89 text

Pure functions are inherently testable

Slide 90

Slide 90 text

But functions make code look awkward!

Slide 91

Slide 91 text

strlen(get_name(get_user($data)))

Slide 92

Slide 92 text

$data |> get_user |> get_name |> strlen

Slide 93

Slide 93 text

pipeline('get_user', 'get_name', 'strlen')

Slide 94

Slide 94 text

But PHP does not properly support functions!

Slide 95

Slide 95 text

use function igorw\edn\parse; $data = parse($edn);

Slide 96

Slide 96 text

PHP 5.6 will hopefully get function autoloading

Slide 97

Slide 97 text

Applicability of the functional paradigm

Slide 98

Slide 98 text

f g h i j k l

Slide 99

Slide 99 text

A foo foo foo foo B foo foo C foo foo D foo foo E foo foo

Slide 100

Slide 100 text

Granularity

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Queue

Slide 103

Slide 103 text

๏ Value semantics ๏ Higher-order functions ๏ Lazy evaluation

Slide 104

Slide 104 text

pure functions

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Questions? • joind.in/{ 9471, 9472 } • github.com/igorw/edn • github.com/nikic/iter • @igorwesome

Slide 107

Slide 107 text

References • The Paradigms of Programming by Robert W. Floyd • Execution in the Kingdom of Nouns by Steve Yegge • The Value of Values by Rich Hickey • Are we there yet? by Rich Hickey