Slide 1

Slide 1 text

FOR BEAUTIFUL APPS FUNCTIONAL PATTERNS

Slide 2

Slide 2 text

IN ELM Oh… it’s Elm, not ELM

Slide 3

Slide 3 text

WHAT WENT WRONG FOR ME

Slide 4

Slide 4 text

WHAT WENT WRONG FOR ME Implementation of the observer pattern in Java. http://classicprogrammerpaintings.com

Slide 5

Slide 5 text

I BECAME SCARED OF PROGRAMMING. Composition over inheritance SOLID SRP Covariance Polymorphism Generics Singleton Inheritance? Dependency Injection DIC Service Locator HammerFactoryFactoryFactory Service Locator Interface “You’re doing it wrong” LSP Inversion of Control “That’s not really OOP”

Slide 6

Slide 6 text

THIS IS NOT FUN ANYMORE. Me, 2014 Despite following all the best practices I kept making dumb mistakes but also felt overwhelmed by them

Slide 7

Slide 7 text

THIS IS NOT FUN ANYMORE. Me, 2014 Despite following all the best practices I kept making dumb mistakes but also felt overwhelmed by them

Slide 8

Slide 8 text

ON A SUDDEN LOOK FOR FRESH AIR Then I found Elm

Slide 9

Slide 9 text

AND IT CERTAINLY LOOKED DELICIOUS.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

} Like a class hierarchy, kind of?

Slide 12

Slide 12 text

} Like a class hierarchy, kind of? } No idea what this is, but seems like it updates a model?

Slide 13

Slide 13 text

} Like a class hierarchy, kind of? } No idea what this is, but seems like it updates a model? }This I can understand

Slide 14

Slide 14 text

} Like a class hierarchy, kind of? } No idea what this is, but seems like it updates a model? }This I can understand Oh, This is pretty cool!

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

“I WILL HAVE TO LEARN THINGS FROM SCRATCH”

Slide 19

Slide 19 text

BUT WAS IT REALLY LIKE LEARNING FROM SCRATCH? Because ain’t nobody got time for that!

Slide 20

Slide 20 text

THIS IS WHAT I NEEDED TO KNOW SO, FUNCTIONAL PROGRAMMING https://xkcd.com/849/

Slide 21

Slide 21 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE JUST MAPPINGS ▸ A function maps an input to an output ▸ Functions can only do one thing: return the mapped value ▸ No accidental “launch ze missiles” in the dotted lines! “a” “dog” “zebra” “a cat” “b” “A” “Dog” “A Cat” “Zebra” “B”

Slide 22

Slide 22 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE JUST MAPPINGS ▸ A function maps an input to an output ▸ Functions can only do one thing: return the mapped value ▸ No accidental “launch ze missiles” in the dotted lines! “a” “dog” “zebra” “a cat” “b” “A” “Dog” “A Cat” “Zebra” “B”

Slide 23

Slide 23 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE JUST MAPPINGS ▸ A function maps an input to an output ▸ Functions can only do one thing: return the mapped value ▸ No accidental “launch ze missiles” in the dotted lines! “a” “dog” “zebra” “a cat” “b” “A” “Dog” “A Cat” “Zebra” “B” f string html

Slide 24

Slide 24 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS CAN DESCRIBE OPERATIONS ▸ Functions are declarative, what seems to be an operation, is just data describing how the operation should work ▸ First Pattern: Effects are “Things” (data) ▸ This data can be used (or not) to change the external world

Slide 25

Slide 25 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour.

Slide 26

Slide 26 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2

Slide 27

Slide 27 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2 Pass an input to a function and get a function back!

Slide 28

Slide 28 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2 f3 f2 Pass an input to a function and get a function back!

Slide 29

Slide 29 text

IT’S FUNCTIONS ALL THE WAY DOWN FUNCTIONS ARE ALSO THINGS ▸ Functions can be passed as input or returned as output ▸ They have a life of their own, they are not attached to things like “classes”. They only add behaviour. f f2 f3 f2 Or pass a function to another and get a value back Pass an input to a function and get a function back!

Slide 30

Slide 30 text

ISN’T THIS DEPENDENCY INJECTION?

Slide 31

Slide 31 text

DEPENDENCY INJECTION, FUNCTIONAL STYLE FIRST PATTERN: FUNCTIONS FOR DEPENDENCY INJECTION ▸ If things seem repetitive, just create new functions after injecting some of the arguments ▸ Corollary: The “interesting” argument of any function should always be the last one

Slide 32

Slide 32 text

FUNCTIONS ARE DEPENDENCY INJECTION TAKEN TO 11

Slide 33

Slide 33 text

DEPENDENCY INJECTION, FUNCTIONAL STYLE

Slide 34

Slide 34 text

DEPENDENCY INJECTION, FUNCTIONAL STYLE These are the kind of things that make functional programming scary

Slide 35

Slide 35 text

DEPENDENCY INJECTION, FUNCTIONAL STYLE LOOKS COMPLICATED HOW TO MAKE IT SIMPLER? These are the kind of things that make functional programming scary

Slide 36

Slide 36 text

DEPENDENCY INJECTION, FUNCTIONAL STYLE LOOKS COMPLICATED HOW TO MAKE IT SIMPLER? } Only these contain runtime data These are the kind of things that make functional programming scary

Slide 37

Slide 37 text

DEPENDENCY INJECTION, FUNCTIONAL STYLE LOOKS COMPLICATED HOW TO MAKE IT SIMPLER? }Identify parameters that are unlikely to change at runtime } Only these contain runtime data These are the kind of things that make functional programming scary

Slide 38

Slide 38 text

DEPENDENCY, FUNCTIONAL STYLE A NEAT STRATEGY.

Slide 39

Slide 39 text

DEPENDENCY, FUNCTIONAL STYLE } Here are the params we wanted to extract out. A NEAT STRATEGY.

Slide 40

Slide 40 text

DEPENDENCY, FUNCTIONAL STYLE } Here are the params we wanted to extract out. } We can now easily create a family of functions with different configurations A NEAT STRATEGY.

Slide 41

Slide 41 text

DEPENDENCY, FUNCTIONAL STYLE } Here are the params we wanted to extract out. } We can now easily create a family of functions with different configurations A NEAT STRATEGY.

Slide 42

Slide 42 text

DEPENDENCY, FUNCTIONAL STYLE } Here are the params we wanted to extract out. } We can now easily create a family of functions with different configurations A NEAT STRATEGY. But what are all those complicated type things?

Slide 43

Slide 43 text

TYPES, GOTTA LOVE THEM ALL! TYPES ARE UNIVERSES ▸ Types describe the universe of things: Universe of number, colours, actions… ▸ You can map from an universe to another using a function ▸ Types are very similar to functions in many aspects. For example they can be composed! ▸ In Elm, types are cheap to create and to compose

Slide 44

Slide 44 text

TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU MODEL THE OUTSIDE WORLD, WITH TYPES

Slide 45

Slide 45 text

TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU MODEL THE OUTSIDE WORLD, WITH TYPES } Types can also describe actions

Slide 46

Slide 46 text

TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU MODEL THE OUTSIDE WORLD, WITH TYPES ▸ The compiler will guide on what to do you whenever you add another case to “Msg”. Time saver! } Types can also describe actions

Slide 47

Slide 47 text

TYPES ARE THINGS IN THE REAL WORLD ELM HELPS YOU MODEL THE OUTSIDE WORLD, WITH TYPES ▸ The compiler will guide on what to do you whenever you add another case to “Msg”. Time saver! ▸ Since the “cases” in a type are closed for extension, you are forced to use “composition over inheritance”. } Types can also describe actions

Slide 48

Slide 48 text

USING TYPES FOR MODELLING WILL GUIDE YOU WHEN REFACTORING

Slide 49

Slide 49 text

USING TYPES FOR MODELLING WILL GUIDE YOU WHEN REFACTORING

Slide 50

Slide 50 text

USING TYPES FOR MODELLING WILL GUIDE YOU WHEN REFACTORING

Slide 51

Slide 51 text

TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS IN THE REAL WORLD

Slide 52

Slide 52 text

TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS IN THE REAL WORLD } Only two types of currencies allowed

Slide 53

Slide 53 text

TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS IN THE REAL WORLD } Only two types of currencies allowed } Explicitly tell the world what types of accounts are possible

Slide 54

Slide 54 text

TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS IN THE REAL WORLD ▸ Types are great for representing your domain. } Only two types of currencies allowed } Explicitly tell the world what types of accounts are possible

Slide 55

Slide 55 text

TYPES ARE THINGS IN THE REAL WORLD TYPES ARE THINGS IN THE REAL WORLD ▸ Types are great for representing your domain. ▸ You can also use Types as documentation. Looking at signatures helps understand what’s possible. } Only two types of currencies allowed } Explicitly tell the world what types of accounts are possible

Slide 56

Slide 56 text

TYPES ARE THINGS IN THE REAL WORLD TYPES CAN ALSO REPRESENT STATES http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html This helps us prevent showing a UI with stale or not relevant feedback to the user }

Slide 57

Slide 57 text

TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF THIS EXPRESSION?

Slide 58

Slide 58 text

TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF THIS EXPRESSION? What if it is empty?

Slide 59

Slide 59 text

TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF THIS EXPRESSION? What if it is empty? What if it is not a string?

Slide 60

Slide 60 text

TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF THIS EXPRESSION? What if it is empty? What if it is not an email? What if it is not a string?

Slide 61

Slide 61 text

TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF THIS EXPRESSION? What if it is empty? What if it is not an email? What if it is not a string?

Slide 62

Slide 62 text

TYPES, GOTTA LOVE THEM ALL! WHAT’S THE RETURN TYPE OF THIS EXPRESSION? What if it is empty? What if it is not an email? What if it is not a string? undefined something something

Slide 63

Slide 63 text

TYPES, GOTTA LOVE THEM ALL! ERROR HANDLING CAN GET UNWIELDY

Slide 64

Slide 64 text

TYPES, GOTTA LOVE THEM ALL! ERROR HANDLING CAN GET UNWIELDY And i’m only dealing with he happy path!

Slide 65

Slide 65 text

https://twitter.com/dr4goonis/status/476617165463105536

Slide 66

Slide 66 text

TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO REPRESENT ERRORS

Slide 67

Slide 67 text

TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO REPRESENT ERRORS Now we have a way to tell the good from the bad.

Slide 68

Slide 68 text

TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO REPRESENT ERRORS Now we have a way to tell the good from the bad. Result error result = This is a built-in type in Elm Err error | OK result

Slide 69

Slide 69 text

TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO REPRESENT ERRORS We wrap the success value in Ok Now we have a way to tell the good from the bad. Result error result = This is a built-in type in Elm Err error | OK result

Slide 70

Slide 70 text

TYPES, GOTTA LOVE THEM ALL! SECOND PATTERN: USE TYPES TO REPRESENT ERRORS We wrap the success value in Ok And the error value in Err Now we have a way to tell the good from the bad. Result error result = This is a built-in type in Elm Err error | OK result

Slide 71

Slide 71 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS

Slide 72

Slide 72 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track

Slide 73

Slide 73 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track

Slide 74

Slide 74 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track

Slide 75

Slide 75 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track

Slide 76

Slide 76 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track

Slide 77

Slide 77 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track

Slide 78

Slide 78 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track Error track

Slide 79

Slide 79 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track Error track

Slide 80

Slide 80 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track Error track

Slide 81

Slide 81 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track Error track

Slide 82

Slide 82 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Ok track Error track

Slide 83

Slide 83 text

TYPES, GOTTA LOVE THEM ALL! TYPES HELP LINEARISE LOGIC FLOWS Most processing flows can be done with this two-tracked “Railway” Ok track Error track

Slide 84

Slide 84 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE REMEMBER THIS? Wouldn’t it be better to make this case impossible from the start?

Slide 85

Slide 85 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE REMEMBER THIS? What if the money is in a different currency than the account? Wouldn’t it be better to make this case impossible from the start?

Slide 86

Slide 86 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE

Slide 87

Slide 87 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE }

Slide 88

Slide 88 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE } c can be anything, in our case, USD or EUR

Slide 89

Slide 89 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE } c can be anything, in our case, USD or EUR

Slide 90

Slide 90 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. } c can be anything, in our case, USD or EUR

Slide 91

Slide 91 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. } c can be anything, in our case, USD or EUR

Slide 92

Slide 92 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. } c can be anything, in our case, USD or EUR

Slide 93

Slide 93 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. } c can be anything, in our case, USD or EUR

Slide 94

Slide 94 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. } c can be anything, in our case, USD or EUR }

Slide 95

Slide 95 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. The parameter c “infects” other types using it. } c can be anything, in our case, USD or EUR }

Slide 96

Slide 96 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. The parameter c “infects” other types using it. } c can be anything, in our case, USD or EUR }

Slide 97

Slide 97 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. The parameter c “infects” other types using it. If all the c’s are not of the same type, the program will not compile! } c can be anything, in our case, USD or EUR }

Slide 98

Slide 98 text

MAKING IMPOSSIBLE THINGS IMPOSSIBLE THIRD PATTERN: MAKE IMPOSSIBLE THINGS IMPOSSIBLE We “parametrised” the type Money. The parameter c “infects” other types using it. If all the c’s are not of the same type, the program will not compile! This is safe to do now } c can be anything, in our case, USD or EUR }

Slide 99

Slide 99 text

COMPILERS BE LIKE

Slide 100

Slide 100 text

COMPILERS BE LIKE

Slide 101

Slide 101 text

PROVIDING IMPLEMENTATIONS DATA IS DATA, FUNCTIONS ARE BEHAVIOUR ▸ Whereas in OOP internals are encapsulated, in Functional programming we often expose them. ▸ Types represent the data, functions operating on types provide the behaviour. ▸ Functions in any module can provide more behaviour to any type of data. List f ?? Date f ?? String -> Date f ??

Slide 102

Slide 102 text

STRATEGY PATTERN, FUNCTIONAL STYLE FOURTH PATTERN: BE GENERIC

Slide 103

Slide 103 text

STRATEGY PATTERN, FUNCTIONAL STYLE FOURTH PATTERN: BE GENERIC

Slide 104

Slide 104 text

STRATEGY PATTERN, FUNCTIONAL STYLE FOURTH PATTERN: BE GENERIC

Slide 105

Slide 105 text

STRATEGY PATTERN, FUNCTIONAL STYLE FOURTH PATTERN: BE GENERIC

Slide 106

Slide 106 text

STRATEGY PATTERN, FUNCTIONAL STYLE FOURTH PATTERN: BE GENERIC

Slide 107

Slide 107 text

STRATEGY PATTERN, FUNCTIONAL STYLE STRATEGY PATTERN, FUNCTIONAL STYLE Use types instead of interfaces!

Slide 108

Slide 108 text

STRATEGY PATTERN, FUNCTIONAL STYLE STRATEGY PATTERN, FUNCTIONAL STYLE Use types instead of interfaces!

Slide 109

Slide 109 text

STRATEGY PATTERN, FUNCTIONAL STYLE STRATEGY PATTERN, FUNCTIONAL STYLE Use types instead of interfaces!

Slide 110

Slide 110 text

STRATEGY PATTERN, FUNCTIONAL STYLE STRATEGY PATTERN, FUNCTIONAL STYLE Use types instead of interfaces!

Slide 111

Slide 111 text

BUT WHAT ABOUT MONADS, MONOIDS AND FUNCTORS…??? Me, constantly Elm made functional programming easy to understand for me. The scary terminology is gone, but the great ideas they express are there

Slide 112

Slide 112 text

THANKS FOR ATTENDING! @jose_zap https://www.xkcd.com/1312/