think functionally
play

Think functionally f(x) = y Presenters Pavel Struhar - PowerPoint PPT Presentation

Think functionally f(x) = y Presenters Pavel Struhar pavel.struhar@accenture.com Twitter: @pavestru Daniel Derevjanik daniel.derevjanik@accenture.com Twitter: @dderevjanik Both from Lightweight Arch. core team Spoiler Everything is a


  1. Think functionally f(x) = y

  2. Presenters Pavel Struhar pavel.struhar@accenture.com Twitter: @pavestru Daniel Derevjanik daniel.derevjanik@accenture.com Twitter: @dderevjanik Both from Lightweight Arch. core team

  3. Spoiler Everything is a function

  4. A monad is just a monoid in the category of endofunctors. What's the problem?

  5. Data and Functions

  6. Data Flow - Expectations f INPUT DATA OUTPUT DATA

  7. Data Flow - Expectations f INPUT DATA OUTPUT DATA PROCESS DATA

  8. Data Flow - Expectations f INPUT DATA OUTPUT DATA

  9. Simplicity is a key

  10. Data Flow - Expectations f INPUT DATA f f f f OUTPUT DATA

  11. Data Flow - Expectations f INPUT DATA f f f f OUTPUT DATA PROCESS DATA

  12. Data Flow - Expectations f INPUT DATA f f f f OUTPUT DATA

  13. Simplicity is a key, still...

  14. Data Flow - Reality class class class CLASS DATA CLASS DATA CLASS DATA method INPUT DATA method method method method method

  15. Data Flow - Reality - OOP in nutshell

  16. Simplicity ? Errr … .

  17. What can we achieve ?

  18. What can we achieve ? - one directional dataflow f f f INPUT DATA OUTPUT DATA

  19. How can we achieve that ?

  20. Separation of Data and Functions

  21. No Hidden Information

  22. No Side-effects

  23. Simple is Pure Function

  24. Pure Function Function without time, (Always the same output for the same arguments.)

  25. Pure Function is Timeless

  26. Pure Function has No side-effects

  27. Pure Function is Opposite to Class Method No this keyword

  28. Why should you care about pure functions ?

  29. Pure functions advantages - Easier to maintain - Testability - Easy to debug - Simply reusable - Thread-safe - One-directional data-flow

  30. Time for Demo

  31. Immutability

  32. Immutability default in Functional languages

  33. Immutability by default in JavaScript?

  34. ECMAScript 6 introduced let keyword

  35. ECMAScript 6 introduced let keyword const also introduced const

  36. Real life: Code reviews

  37. Immutable Data Structures FTW

  38. Immutable building blocks ● Records ● Vectors ● Maps ● Sets … build anything you want

  39. Tree data structure

  40. We want to change one node

  41. ...or keep it for the history record

  42. Immutability in Java

  43. Practices for immutability in Java (make them habits) ● Mark the class final ● Mark all the fields private and final ● Force all the callers to construct an object of the class directly, i.e. do not use any setter methods ● Do not change the state of the objects in any methods of the class

  44. “Classes should be immutable unless there's a very good reason to make them mutable.... If a class cannot be made immutable, limit its mutability as much as possible.” -- Joshua Bloch (taken from the book Effective Java)

  45. Benefits of immutable objects ● Thread-safety ● Easier to parallelize ● Consistent internal state (in spite of exception) ● References to immutable objects can be cached ○ Easy to implement UNDO - REDO functionality

  46. Time for a success story

  47. Redux.js ● FLUX-ish library by Dan Abramov ○ Creator of Hot Module Reloading for ReactJS ● Motivation: ○ We want Hot Module Reloading not only for View Components ○ but also for Business logic inside FLUX stores (like Models in MVC)

  48. Redux ● Solution ○ Take state out of Stores ○ Make Store methods immutable / pure functions ○ No classes, just functions - reducers

  49. Redux ● Result ○ HMR also for reducers ○ Application keeps state after HMR !!! ○ Time travelling ○ Awesome debugging tools ○ Community excitement - this is how FLUX was supposed to work from the start

  50. Summary

  51. What can we improve in our daily programming ?

  52. Shared mutable state is the root of all evil. Pete Hunt, ReactJS team @ Facebook

  53. Avoid mutability

  54. Don’t use global scoped variables

  55. Make const not var

  56. Separate Data from Business logic

  57. Use Pure functions

  58. Learning Haskell will help you write better Java(Script) code

  59. OOP vs FP ?

  60. Composition over Inheritance

  61. Topics for future sessions Functional programming ● Higher order functions ● Partial application ● Currying ● Closures ● Functors ● … ● Monads?

  62. Topics for future sessions Functional Reactive Programming ● ReactiveX (RxJS, RxJava), … ○ underscore / lodash for streams of events / data ● Observables ● Reactive (user) interfaces ● …

  63. Topics for future sessions Clojure Haskell Ocaml Elm ClojureScript PureScript ReasonML

  64. Happy building

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend