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 in practice

872 views

Published on

Lecture demistifying monads. After talking a bit about Monoids and Functor I explain a functional design technics which is Monad through the refactoring of a JDBC code.
Talk given at The Developper Conference in Porto Alegre in 2014 (http://www.thedevelopersconference.com.br),

Code source of the presentation is in my github: https://github.com/toff63/monads-in-practice-tdc

Published in: Software
  • Be the first to comment

Monads in practice

  1. 1. Monads in Practice Christophe Marchal | Software Architect
  2. 2. A couple of links christophe.marchal@ilegra.com @toff63 http://github.com/toff63 http://francesbagual.net
  3. 3. Disclaimer
  4. 4. Manage complexity with Monad
  5. 5. Study Monads online is hard
  6. 6. Academic talks Functor Monoids Monads Category theory
  7. 7. Category Theory
  8. 8. Function Monoids Associative ( 1 + 2 ) + 3 = 1 + ( 2 + 3 ) InitiAl Value Result stay in the same category
  9. 9. Monoid Example
  10. 10. Monoid
  11. 11. Functor Things that can be map over capacity BEHaviour map
  12. 12. Functor: Example
  13. 13. Functor: Example
  14. 14. Monads
  15. 15. Problem it solves
  16. 16. Scenario F 1 F 2
  17. 17. sweet Monads Rules: function to create it
  18. 18. Broken Composition F 1 F 2 sweet
  19. 19. Monads Rules: You need a Functor F 1 map ( f 2 ) sweet
  20. 20. Scenario F 1 F 3 sweet
  21. 21. Problem: Nested Monad F 1 sweet map ( f 3 ) sweet
  22. 22. F 1 sweet Flatmap ( f 3 ) Monads Rules: You need a Flatmap
  23. 23. Business rule TRUE False age > 18 Authorized Not Authorized
  24. 24. Business rule implementation
  25. 25. What we want
  26. 26. Monad (?) Code
  27. 27. Trade-off More code than before Better API Easy to reuse Potential complexity Managed
  28. 28. Traditional Code
  29. 29. Side Effect Our method can return either a sequence either null
  30. 30. Exist ? TRUE False null ? Riendutout quelquechose
  31. 31. Encapsulating if null with Option
  32. 32. Creating our OptionBagual
  33. 33. Code with Options
  34. 34. Business code
  35. 35. Side Effect Our method can return either an option [sequence] either throw an exception
  36. 36. Try! Success Failure Try to execute Gremio Inter
  37. 37. Encapsulating try/catch with Try
  38. 38. Encapsulating try/catch with Try
  39. 39. Code with Try
  40. 40. Business code
  41. 41. Connection Handling
  42. 42. No silver bullet
  43. 43. Loan Pattern
  44. 44. Final Code
  45. 45. Conclusion ● NO IFs ● Border cases handled on demand ● More declarative types ● Highlight business code ● Easy to create ● More reusable code
  46. 46. Thank You! Christophe Marchal | Software Architect

×