Slide 1

Slide 1 text

Functional Reactive Programming @MariusBudin

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Is this something new I should know about?

Slide 4

Slide 4 text

it’s NOT new

Slide 5

Slide 5 text

and YES, you should know about it

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

We’ll be talking about • Reactive programming • Functional programming • Why are they so cool together • How to cook a lasagna 45’

Slide 8

Slide 8 text

Reactive Programming

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Yeah, that’s still 6

Slide 11

Slide 11 text

Yeah, that’s still 6

Slide 12

Slide 12 text

Yeah, that’s still 6

Slide 13

Slide 13 text

The value ‘10’ assigned to a is never used Yeah, that’s still 6

Slide 14

Slide 14 text

Reactive programming means this is true FOREVER

Slide 15

Slide 15 text

RP implies you have a datatype representing a value over time

Slide 16

Slide 16 text

If a changes over time and b = a + 1; then b will change over time

Slide 17

Slide 17 text

It’s actually that simple*

Slide 18

Slide 18 text

It’s actually that simple* * it’s not

Slide 19

Slide 19 text

Continuous processing: updating a display of changing data Coordinating concurrent computations is difficult and can involve complex synchronization patterns Long story…

Slide 20

Slide 20 text

State changes over time, and there are typically dependencies between data When one thing changes, the programmer must update everything that depends on it. Done by hand, this is a tedious and error-prone task. Long story…

Slide 21

Slide 21 text

Reacting to changes usually implies using callbacks, routines that perform imperative operations in response to events. Coordination within the resulting “callback soup” can be difficult, since numerous isolated code fragments end up manipulating the same data. Long story…

Slide 22

Slide 22 text

All* problems gone if this is true FOREVER Short story…

Slide 23

Slide 23 text

Functional Programming

Slide 24

Slide 24 text

It’s all about side-effects

Slide 25

Slide 25 text

NO side-effects

Slide 26

Slide 26 text

–Robert C. Martin (Uncle Bob) “Functional programming is programming without assignment statements.”

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Each loop causes the variable i to take a new value

Slide 29

Slide 29 text

Each loop causes the variable i to take a new value This is assignment

Slide 30

Slide 30 text

The functional way (no assignments)

Slide 31

Slide 31 text

The functional way (no assignments) This is actually valid Clojure code :)

Slide 32

Slide 32 text

The functional way (no assignments) The program takes the first 25 elements of a list of the squares of sequential integers starting at 1

Slide 33

Slide 33 text

Referential Transparency The program takes the first 25 elements of a list of the squares of sequential integers starting at 1

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Referential Transparency The program takes the first 25 elements of a list of the squares of sequential integers starting at 1

Slide 36

Slide 36 text

Referential Transparency The program takes the first 25 elements of a list of the squares of sequential integers starting at 1 you can replace the words in that sentence with their definitions, and not change the meaning

Slide 37

Slide 37 text

take(25, (squares-of (integers))) (1 4 9 16 25 36 49 64 ... 576 625) you can replace the words in that sentence with their definitions, and not change the meaning

Slide 38

Slide 38 text

take(25, (squares-of (1 2 3 …))) (1 4 9 16 25 36 49 64 ... 576 625) you can replace the words in that sentence with their definitions, and not change the meaning

Slide 39

Slide 39 text

take(25, (1 4 9 16 25 36 49…)) (1 4 9 16 25 36 49 64 ... 576 625) you can replace the words in that sentence with their definitions, and not change the meaning

Slide 40

Slide 40 text

(1 4 9 16 25 36 49 64 ... 576 625) (1 4 9 16 25 36 49 64 ... 576 625) you can replace the words in that sentence with their definitions, and not change the meaning

Slide 41

Slide 41 text

every time you invoke a particular function call, you will get the same result

Slide 42

Slide 42 text

every time you invoke a particular function call, you will get the same result Functional Programming

Slide 43

Slide 43 text

mathematical functions f(x) = x+2 Functional Programming

Slide 44

Slide 44 text

no changing-state no mutable data Functional Programming

Slide 45

Slide 45 text

Characteristic Imperative Functional Programmer focus How to perform tasks (algorithms) and how to track changes in state. What information is desired and what transformations are required. State changes Important Non-existent. Order of execution Important Low importance. Primary flow control Loops, conditionals, and function (method) calls. Function calls, including recursion. Primary manipulation unit Instances of structures or classes. Functions as first-class objects and data collections

Slide 46

Slide 46 text

Characteristic Imperative Functional Programmer focus How to perform tasks (algorithms) and how to track changes in state. What information is desired and what transformations are required. State changes Important Non-existent. Order of execution Important Low importance. Primary flow control Loops, conditionals, and function (method) calls. Function calls, including recursion. Primary manipulation unit Instances of structures or classes. Functions as first-class objects and data collections

Slide 47

Slide 47 text

Print the second string containing a “2” in upper case from an array of strings, print “none” if none found and log the error if something bad happens

Slide 48

Slide 48 text

Print the second string containing a “2” in upper case from an array of strings, print “none” if none found and log the error if something bad happens

Slide 49

Slide 49 text

Print the second string containing a “2” in upper case from an array of strings, print “none” if none found and log the error if something bad happens

Slide 50

Slide 50 text

Print the second string containing a “2” in upper case from an array of strings, print “none” if none found and log the error if something bad happens

Slide 51

Slide 51 text

Print the second string containing a “2” in upper case from an array of strings, print “none” if none found and log the error if something bad happens

Slide 52

Slide 52 text

Print the second string containing a “2” in upper case from an array of strings, print “none” if none found and log the error if something bad happens

Slide 53

Slide 53 text

Benefits of FP • Composability -> self-contained and stateless • Increased readability and maintainability -> each function is designed to accomplish a specific task given its arguments. The function does not rely on any external state. • Easier reiterative development -> the code is easier to refactor • Easier testing -> pure functions can more easily be tested in isolation, you can write test code that calls the pure function with typical values, valid edge cases, and invalid edge cases.

Slide 54

Slide 54 text

Functional Reactive Programming The Next Big Thing ®

Slide 55

Slide 55 text

FRP RP FP

Slide 56

Slide 56 text

FP • Based on functions (the mathematical sense of the word) • Deliberately avoids shared mutable state • Implies the use of inmutable data structures • Emphasizes composability Recap

Slide 57

Slide 57 text

RP • It’s event-based • Acts to input • Is a flow of data (value over time) Recap

Slide 58

Slide 58 text

FRP

Slide 59

Slide 59 text

FRP Love the bacon, love the ice cream…

Slide 60

Slide 60 text

not a methodology

Slide 61

Slide 61 text

A programming technique to improve your code in an area that just happens to be a common source of complexity (and therefore bugs): event propagation. What it IS

Slide 62

Slide 62 text

What it IS • A replacement for the observer pattern (callbacks/listeners) • A composable, modular way to code event-driven logic. • A way to bring order to the management of program state • A different way of thinking: The program is expressed as a reaction to its inputs, or as a flow of data.

Slide 63

Slide 63 text

• A replacement for the observer pattern (callbacks/listeners) • A composable, modular way to code event-driven logic. • A way to bring order to the management of program state • A different way of thinking: The program is expressed as a reaction to its inputs, or as a flow of data. What it IS what’s wrong with this?

Slide 64

Slide 64 text

Sources of bugs in listeners • Unpredictable order -> the order in which events are received can depend on the order you registered the listeners in. FRP makes the order not matter in most cases, and it makes it predictable in the rest. • Missed first event -> It can be difficult to guarantee that you have registered your listeners before you send the first event. FRP is transactional, so it is possible to provide this guarantee. • Messy state -> Callbacks push your code into a traditional state machine style, which gets messy fast. FRP brings order. • Threading issues -> Attempting to make listeners thread-safe can lead to deadlocks. FRP eliminates this issue. • Accidental recursion -> The order in which you update local state and notify listeners can be critical, and it's easy to make mistakes. FRP eliminates this issue.

Slide 65

Slide 65 text

FRP will appear a bit strange simple != easy to understand Paradigm shift

Slide 66

Slide 66 text

Thinking declaratively: What the program is, not what it does The key

Slide 67

Slide 67 text

Cook lasagna 1. Heat the oil in a large pan. 2. Fry the onions until golden. 3. Add ground beef and tomato. …. What your mind is telling you

Slide 68

Slide 68 text

Cook lasagna • Lasagna is grated cheese on cheese sauce on flat pasta on cheese sauce on bolognese on flat pasta on cheese sauce on bolognese on flat pasta on cheese sauce baked for 45 minutes. • Bolognese is onion and oil fried until golden mixed with ground beef mixed with tomato simmered for 20 minutes. • Cheese sauce is milk and cheese added progressively to roux while frying it until the sauce thickens. • Roux is flour and butter fried briefly. • Baked is put in an oven dish in a hot oven. • Fried is put in a pan on high and mixed frequently. • Simmered is put in a pan on low and mixed infrequently. The a-ha! moment

Slide 69

Slide 69 text

Cook lasagna • Lasagna is grated cheese on cheese sauce on flat pasta on cheese sauce on bolognese on flat pasta on cheese sauce on bolognese on flat pasta on cheese sauce baked for 45 minutes. • Bolognese is onion and oil fried until golden mixed with ground beef mixed with tomato simmered for 20 minutes. • Cheese sauce is milk and cheese added progressively to roux while frying it until the sauce thickens. • Roux is flour and butter fried briefly. • Baked is put in an oven dish in a hot oven. • Fried is put in a pan on high and mixed frequently. • Simmered is put in a pan on low and mixed infrequently. The a-ha! moment

Slide 70

Slide 70 text

bon appetit!

Slide 71

Slide 71 text

thanks @MariusBudin