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

11 views

Published on

Are you curious about functional programming concepts, but daunted by its weird vocabulary? Have you heard of Monads, but struggled to understand what they are? Should you care at all?

In this session I will explain Monads using C# and Java classes. I’ll start with “Why” part, and then will give enough of clear and concise examples that you will be able to bring home and start using in your functional journey.

Functional programming is not complicated, it’s just unfamiliar. Let’s bring some clarity today.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Monads Explained for OOP Developers

  1. 1. Monads Explained for OOP Developers NextBuild Eindhoven | September 29 | 2018 Mikhail Shilkov
  2. 2. MONAD MONOID CATEGORY ENDOFUNCTORS
  3. 3. “ “ Douglas Crockford, who found Good Partsin Javascript Monads are cursed — once you understand monads for yourself you lose the ability to explain them to others
  4. 4. “ “ Tomas Petricek, The Alan Turing Institute http://tomasp.net/academic/pa pers/monads/monads- programming.pdf Learning monads and writing a monad tutorial 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() { ... } }
  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. class Speaker { Talk nextTalk() { ... } } class Talk { Conference getConference() { ... } } class Conference { City getCity() { ... } }
  11. 11. Speaker Nullable<Talk> nextTalk Talk Nullable<Conference> getConference Conference Nullable<City> getCity
  12. 12. Nullable<City> nextTalkCity Speaker Talk? nextTalk Conference? getConference City? getCity
  13. 13. Nullable<City> nextTalkCity Speaker Talk? nextTalk Conference? getConference City? getCity
  14. 14. Nullable<City> nextTalkCity Speaker nextTalk getConference getCity
  15. 15. Speaker Optional<Talk> nextTalk Talk Optional<Conference> getConference Conference Optional<City> getCity
  16. 16. Optional<City> nextTalkCity Speaker nextTalk getConference getCity
  17. 17. Optional<City> nextTalkCity Speaker nextTalk getConference getCity
  18. 18. Speaker List<Talk> getTalks Talk List<Conference> getConferences Conference List<City> getCities
  19. 19. List<City> allCitiesToVisit Speaker List<City> Talk getTalks Conference getConferences City 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. List<City> allCitiesToVisit Speaker getTalks getConferences getCities
  24. 24. Speaker Task<Talk> nextTalk Talk Task<Conference> getConference Conference Task<City> getCity
  25. 25. Task<City> nextTalkCity Speaker nextTalk getConference getCity
  26. 26. Task<City> nextTalkCity Speaker nextTalk getConference getCity
  27. 27. CompletableFuture<City> nextTalkCity Speaker nextTalk getConference getCity
  28. 28. CompletableFuture<City> nextTalkCity Speaker nextTalk getConference getCity
  29. 29. PATTERN
  30. 30. nextTalk getConference getCity // Optional<T> getTalks getConferences getCities // List<T> nextTalk getConference getCity // CompletableFuture<T>
  31. 31. WorkflowThatReturns<T> WorkflowThatReturns<U> addStep T WorkflowThatReturns<U>
  32. 32. WorkflowThatReturns<Phone> workflow Speaker nextTalk getConference getCity
  33. 33. Monad<T> Monad<U> bind T Monad<U> // or alternatively Monad<T> Monad<U> apply T U Monad<U> unwrap Monad<Monad<U>>
  34. 34. Optional<T> // ... constructors go here Optional<U> flatMap T Optional<U> func func Optional
  35. 35. Enumerable IEnumerable<U> SelectMany IEnumerable<T> T IEnumerable<U> func func
  36. 36. CompletableFuture CompletableFuture<U> thenCompose T CompletableFuture<U>> func // Some black magic happens here, // but it works!!!
  37. 37. WORKFLOW
  38. 38. Talk talk talk talk talk
  39. 39. async talk await await talk await talk
  40. 40. MONADS
  41. 41. nextTalkCity async let! nextTalk let! getConference let! getCity
  42. 42. nextTalkCity option let! nextTalk let! getConference let! getCity
  43. 43. actor let! return!
  44. 44. OptionBuilder Return Bind func func
  45. 45. MONAD
  46. 46. • Pattern • Generic Container • Workflow Builder
  47. 47. “ “ 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
  48. 48. Monads explained in C#
  49. 49. Monads explained in Kotlin
  50. 50. F#P Eindhoven Meetup
  51. 51. @MikhailShilkov https://mikhail.io

×