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

Monads @ ATLRUG

Monads @ ATLRUG

An introduction to monads for Rubyists, with accompanying code at https://github.com/jamesdabbs/rb-monads

James Dabbs

August 14, 2013
Tweet

More Decks by James Dabbs

Other Decks in Technology

Transcript

  1. What Is a Monad? Definition 1: A monad is a

    monoid in the category of endofunctors
  2. Abstractioneering Connection details are abstracted out so they can be

    changed in one place The User object encapsulates a user’s behavior
  3. Abstractioneering Connection details are abstracted out so they can be

    changed in one place The User object encapsulates a user’s behavior Separation of data and representation
  4. Abstractioneering The real value of Rails (or any MV* framework)

    is in providing a set of useful abstractions to solve common problems and promote code reuse.
  5. Abstractioneering The real value of Rails (or any MV* framework)

    is in providing a set of useful abstractions to solve common problems and promote code reuse. Our goal as software engineers should be to recognize common problems and write reusable abstractions to solve them.
  6. Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads
  7. Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads
  8. Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads Object oriented
  9. Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads Object oriented Functional
  10. Functional Programming Start with functions - these represent actions or

    behaviors Build programs by combining simple functions into more complicated ones
  11. Functional Programming Start with functions - these represent actions or

    behaviors Build programs by combining simple functions into more complicated ones Verbs, not nouns
  12. Functional Programming Category theory is the mathematical study of functions

    and composition Monads are important objects in category theory
  13. Functional Programming Category theory is the mathematical study of functions

    and composition Monads are important objects in category theory As such, monads tend to be more important and prevalent in strongly functional languages.
  14. Functional Programming A language that doesn't affect the way you

    think about programming, is not worth knowing. For instance, Haskell programs can’t do anything without monads
  15. Functional Programming A language that doesn't affect the way you

    think about programming, is not worth knowing. - Alan Perlis For instance, Haskell programs can’t do anything without monads
  16. What Is a Monad? Definition 2: A monad is the

    glue you need to bind these functions together and “cross-compose”
  17. What Is a Monad? Definition 2: A monad is the

    glue you need to bind these functions together and “cross-compose”
  18. What Is a Monad? Definition 3: A monad is a

    structure m with two functions*
  19. What Is a Monad? Definition 3: A monad is a

    structure m with two functions*
  20. What Is a Monad? Definition 3: A monad is a

    structure m with two functions* In the case of IO, this becomes
  21. What Is a Monad? Definition 3: A monad is a

    structure m with two functions* In the case of IO, this becomes
  22. Monad Laws We need to specify a few restrictions This

    bind totally disregards the bound value
  23. Monad Laws The last law looks complicated, but that’s because

    we’re at the wrong level of abstraction. With an appropriate cross-composition operator it’s
  24. Monad Laws The last law looks complicated, but that’s because

    we’re at the wrong level of abstraction. With an appropriate cross-composition operator it’s
  25. The Ubiquitous Monad IO - as discussed, and hence the

    do joke List - represents non-deterministic (or, with a little modification, probabilistic) computation
  26. The Ubiquitous Monad IO - as discussed, and hence the

    do joke List - represents non-deterministic (or, with a little modification, probabilistic) computation Maybe - represents computation that might fail to return a value
  27. The Ubiquitous Monad IO - as discussed, and hence the

    do joke List - represents non-deterministic (or, with a little modification, probabilistic) computation Maybe - represents computation that might fail to return a value State - functions that alter program state
  28. The Ubiquitous Monad IO - as discussed, and hence the

    do joke List - represents non-deterministic (or, with a little modification, probabilistic) computation Maybe - represents computation that might fail to return a value State - functions that alter program state Persistent - functions that alter a database
  29. Takeaways Monads are a different type of composition, well suited

    to managing state, history, or side effects
  30. Takeaways Monads are a different type of composition, well suited

    to managing state, history, or side effects Functional programming and category theory are worth studying - especially together
  31. References Source code - https://github.com/jamesdabbs/rb- monads The Kingdom of Nouns

    - http://steve- yegge.blogspot.com/2006/03/execution-in-kingdom- of-nouns.html Learn You a Haskell - http://learnyouahaskell.com/