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

Mo'Problems, Mo'Nads

Mo'Problems, Mo'Nads

Monads; just that word alone probably either made you shudder or roll your eyes. But don't run away!

Functional Programming (FP) offers many benefits to our programs, even in a loose/flexible language like JS. But we often avoid them just because of intimidating terminology, notation, and math theory. Rarely is this more prevalent than with monads.

This talk is not a dissertation on monads and broader category theory. We're merely dipping our toe into the shallow end here, not plunging head first into the deep end. But I hope maybe it entices you to swim around, because the water's actually quite pleasant!

We'll look at familiar problems in code that we typically solve imperatively, and then see how some of the de-mystified behaviors associated with monads (and friends!) can help. After this talk, I hope you'll look more closely at monads and other algebraic structures.

Topics illustrated: selecting values with conditional logic, forking behavior on exceptions, and juggling asynchronous side effects.

Kyle Simpson
PRO

November 08, 2020
Tweet

More Decks by Kyle Simpson

Other Decks in Programming

Transcript

  1. Mo’Problems, Mo’nads
    getify (kyle simpson)

    View Slide

  2. "Some people, when confronted
    with a problem, think 'I know, I'll
    use regular expressions.' Now they
    have two problems."
    -- Jamie Zawinski

    View Slide

  3. regexes get a bad rap

    View Slide

  4. View Slide

  5. “right tool … blah blah…”
    specialized vs generalized

    View Slide

  6. "Technology is a word that
    describes something that
    doesn't work yet."
    -- Douglas Adams

    View Slide

  7. Monads
    unit(a) >>= λx → f(x) ↔ f(a)
    ma >>= λx → unit(x) ↔ ma
    ma >>= λx → (f(x) >>= λy → g(y)) ↔ (ma >>= λx → f(x)) >>= λy → g(y)

    View Slide

  8. "a monad is just a monoid in the
    category of endofunctors."

    View Slide

  9. "algebras, and categories, and
    type theory… oh my!"

    View Slide

  10. View Slide

  11. there’s only
    one right way
    to explain
    monads!

    View Slide

  12. types vs values

    View Slide

  13. types: behaviors we can expect
    values: behaviors we can use

    View Slide

  14. what does monad mean?
    the Monad (type/interface) vs
    a monad (value)

    View Slide

  15. the Monad: 3 laws
    a monad: data structure

    View Slide

  16. https://github.com/getify/Monio

    View Slide

  17. View Slide

  18. monads & friends

    View Slide

  19. View Slide

  20. why monads?

    View Slide

  21. 1. choose a value or fallback?
    2. gracefully handle exceptions?
    3. manage side effects?
    how could I...

    View Slide

  22. 1. choose a value or fallback?
    Maybe Monad

    View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. 2. gracefully handle exceptions?
    Either Monad

    View Slide

  27. View Slide

  28. 3. manage side effects?
    IO Monad

    View Slide

  29. View Slide

  30. View Slide

  31. https://codepen.io/getify/pen/VwjyoMY?editors=0011
    3. IO + Maybe + Either + ...

    View Slide

  32. View Slide