Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Reactive Programming

Functional Reactive Programming

A general introduction to Functional and Reactive programming

MariusBudin

June 05, 2015
Tweet

More Decks by MariusBudin

Other Decks in Programming

Transcript

  1. We’ll be talking about • Reactive programming • Functional programming

    • Why are they so cool together • How to cook a lasagna 45’
  2. If a changes over time and b = a +

    1; then b will change over time
  3. Continuous processing: updating a display of changing data Coordinating concurrent

    computations is difficult and can involve complex synchronization patterns Long story…
  4. 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…
  5. 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…
  6. The functional way (no assignments) The program takes the first

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

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

    a list of the squares of sequential integers starting at 1
  9. 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
  10. 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
  11. 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
  12. 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
  13. (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
  14. every time you invoke a particular function call, you will

    get the same result Functional Programming
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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.
  24. 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
  25. RP • It’s event-based • Acts to input • Is

    a flow of data (value over time) Recap
  26. FRP

  27. 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
  28. 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.
  29. • 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?
  30. 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.
  31. 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
  32. 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
  33. 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