Monads @ ATLRUG

Monads @ ATLRUG

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

908eb2dedca30a0209477244e069b22c?s=128

James Dabbs

August 14, 2013
Tweet

Transcript

  1. 1.

    An Intro to Monads Categories for the Working Programmer James

    Dabbs jdabbs@emcien.com | github.com/jamesdabbs
  2. 4.

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

    monoid in the category of endofunctors
  3. 5.
  4. 16.

    Abstractioneering Connection details are abstracted out so they can be

    changed in one place The User object encapsulates a user’s behavior
  5. 17.

    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
  6. 20.

    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.
  7. 21.

    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.
  8. 26.
  9. 27.

    Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads
  10. 28.

    Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads
  11. 29.

    Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads Object oriented
  12. 30.

    Reusing Code Inheritance / subclassing Modules / mixins Object composition

    / decorators / presenters Function composition Monads Object oriented Functional
  13. 33.

    Functional Programming Start with functions - these represent actions or

    behaviors Build programs by combining simple functions into more complicated ones
  14. 34.

    Functional Programming Start with functions - these represent actions or

    behaviors Build programs by combining simple functions into more complicated ones Verbs, not nouns
  15. 37.

    Functional Programming Category theory is the mathematical study of functions

    and composition Monads are important objects in category theory
  16. 38.

    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.
  17. 40.

    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
  18. 41.

    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
  19. 53.

    What Is a Monad? Definition 2: A monad is the

    glue you need to bind these functions together and “cross-compose”
  20. 54.

    What Is a Monad? Definition 2: A monad is the

    glue you need to bind these functions together and “cross-compose”
  21. 57.

    What Is a Monad? Definition 3: A monad is a

    structure m with two functions*
  22. 58.

    What Is a Monad? Definition 3: A monad is a

    structure m with two functions*
  23. 59.

    What Is a Monad? Definition 3: A monad is a

    structure m with two functions* In the case of IO, this becomes
  24. 60.

    What Is a Monad? Definition 3: A monad is a

    structure m with two functions* In the case of IO, this becomes
  25. 63.

    Monad Laws We need to specify a few restrictions This

    bind totally disregards the bound value
  26. 68.

    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
  27. 69.

    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
  28. 72.

    The Ubiquitous Monad IO - as discussed, and hence the

    do joke List - represents non-deterministic (or, with a little modification, probabilistic) computation
  29. 73.

    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
  30. 74.

    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
  31. 75.

    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
  32. 77.
  33. 78.

    Takeaways Monads are a different type of composition, well suited

    to managing state, history, or side effects
  34. 79.

    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
  35. 81.

    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/