Share
Embed
Functional Reactive Programming
A general introduction to Functional and Reactive programming
MariusBudin
June 05, 2015
Transcript
71 slides
-
We’ll be talking about • Reactive programming • Functional programming
• Why are they so cool together • How to cook a lasagna 45’ -
The value ‘10’ assigned to a is never used Yeah,
that’s still 6 -
If a changes over time and b = a +
1; then b will change over time -
Continuous processing: updating a display of changing data Coordinating concurrent
computations is difficult and can involve complex synchronization patterns Long story… -
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… -
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… -
–Robert C. Martin (Uncle Bob) “Functional programming is programming without
assignment statements.” -
Each loop causes the variable i to take a new
value This is assignment -
The functional way (no assignments) The program takes the first
25 elements of a list of the squares of sequential integers starting at 1 -
Referential Transparency The program takes the first 25 elements of
a list of the squares of sequential integers starting at 1 -
Referential Transparency The program takes the first 25 elements of
a list of the squares of sequential integers starting at 1 -
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 -
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 -
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 -
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 -
(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 -
every time you invoke a particular function call, you will
get the same result -
every time you invoke a particular function call, you will
get the same result Functional Programming -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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. -
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 -
RP • It’s event-based • Acts to input • Is
a flow of data (value over time) Recap -
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 -
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. -
• 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? -
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. -
FRP will appear a bit strange simple != easy to
understand Paradigm shift -
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 -
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 -
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