Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Monads Explained for OOP Developers

416 views

Published on

Monads Explained for Object-Oriented Programming Developers at Codemotion Rome 2018

  • Hey guys! Who wants to chat with me? More photos with me here 👉 http://www.bit.ly/katekoxx
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Monads Explained for OOP Developers

  1. 1. MONADS Explained for OOP Developers by Mikhail Shilkov ROME - APRIL 13/14 2018
  2. 2. MONAD MONOID CATEGORY ENDOFUNCTORS
  3. 3. “ “ Douglas Crockford, who found Good Parts in Javascript Monads are cursed - once you understand monads for yourself you lose the ability to explain them to others
  4. 4. “ “ TomasPetricek, TheAlanTuringInstitute http://tomasp.net/acade mic/papers/monads/mon ads-programming.pdf Learning monads and writing a monad tutorial or writing a talk on monads has also become an important milestone in learning about theory of functional programming.
  5. 5. // Type annotation func: TypeA -> TypeB // Static methods static class Mapper { static ClassB func(ClassA a) { ... } } // Instance methods class ClassA { ClassB func() { ... } }
  6. 6. class Speaker { Talk nextTalk() { ... } } class Talk { Conference getConference() { ... } } class Conference { City getCity() { ... } } class City { ... }
  7. 7. static City nextTalkCity(Speaker speaker) { Talk talk = speaker.nextTalk(); Conference conf = talk.getConference(); City city = conf.getCity(); return city; }
  8. 8. City nextTalkCity Speaker nextTalk getConference getCity
  9. 9. NULL
  10. 10. Speaker Nullable<Talk> nextTalk Talk Nullable<Conference> getConference Conference Nullable<City> getCity
  11. 11. Nullable<City> nextTalkCity Speaker Talk? nextTalk Conference? getConference City? getCity
  12. 12. Nullable<City> nextTalkCity Speaker Talk? nextTalk Conference? getConference City? getCity
  13. 13. Nullable<City> nextTalkCity Speaker nextTalk getConference getCity
  14. 14. Speaker Optional<Talk> nextTalk Talk Optional<Conference> getConference Conference Optional<City> getCity
  15. 15. Optional<City> nextTalkCity Speaker nextTalk getConference getCity
  16. 16. Optional<City> nextTalkCity Speaker nextTalk getConference getCity
  17. 17. Speaker List<Talk> getTalks Talk List<Conference> getConferences Conference List<City> getCities
  18. 18. List<City> allCitiesToVisit Speaker List<City> Talk getTalks Conference getConferences City getCities
  19. 19. List<City> allCitiesToVisit Speaker getTalks getConferences getCities
  20. 20. List<City> allCitiesToVisit Speaker getTalks getConferences getCities
  21. 21. List<City> allCitiesToVisit Speaker getTalks getConferences getCities
  22. 22. List<City> allCitiesToVisit Speaker getTalks getConferences getCities
  23. 23. Speaker Task<Talk> nextTalk Talk Task<Conference> getConference Conference Task<City> getCity
  24. 24. Task<City> nextTalkCity Speaker nextTalk getConference getCity
  25. 25. Task<City> nextTalkCity Speaker nextTalk getConference getCity
  26. 26. CompletableFuture<City> nextTalkCity Speaker nextTalk getConference getCity
  27. 27. CompletableFuture<City> nextTalkCity Speaker nextTalk getConference getCity
  28. 28. PATTERN
  29. 29. nextTalk getConference getCity // Optional<T> getTalks getConferences getCities // List<T> nextTalk getConference getCity // CompletableFuture<T>
  30. 30. WorkflowThatReturns<T> WorkflowThatReturns<U> addStep Function T WorkflowThatReturns<U>
  31. 31. WorkflowThatReturns<Phone> workflow Speaker nextTalk getConference getCity
  32. 32. Monad<T> Monad<U> bind T Monad<U> // or alternatively Monad<T> Monad<U> map T U Monad<U> flatten Monad<Monad<U>>
  33. 33. Optional<T> // ... constructors go here Optional<U> flatMap T Optional<U> func func Optional
  34. 34. Enumerable IEnumerable<U> SelectMany IEnumerable<T> T IEnumerable<U> func func
  35. 35. CompletableFuture CompletableFuture<U> thenCompose T CompletableFuture<U>> func // Some black magic happens here, // but it works!!!
  36. 36. WORKFLOW
  37. 37. Talk talk talk talk talk
  38. 38. async talk await await talk await talk
  39. 39. MONADS
  40. 40. nextTalkCity async let! nextTalk let! getConference let! getCity
  41. 41. nextTalkCity option let! nextTalk let! getConference let! getCity
  42. 42. actor let! return!
  43. 43. MONAD
  44. 44. • To apply same approach to new problems • To talk to other developers • It’s already in your language • It’s getting used more
  45. 45. • Pattern • Generic Container • Workflow Builder
  46. 46. “ “ John De Goes, who knows what Monads are Monads are virtual machines for expressing sequential, dependent computation, where the instruction set of a VM is given by the structure of the monad's constructors

×