Lightning - Monads you already use (without knowing it)

Lightning - Monads you already use (without knowing it)

The lightning talk version of this talk.

9245f8593dc19a394a021ea0f17f9e0f?s=128

Tejas Dinkar

July 19, 2014
Tweet

Transcript

  1. Monads you’ve already put in prod Tejas Dinkar Nilenso Software

  2. about.me • Hi, I’m Tejas • Nilenso: Partner • twitter:

    tdinkar • github: gja
  3. If you think you understand Monads, you don't understand Monads.

  4. This talk is inaccurate and will make a mathematician cry

  5. Monads • Programmable Semicolons • Used to hide plumbing away

    from you • Monads are just monoids in the category of endofunctors
  6. None
  7. Values Value

  8. Monads Value Box

  9. Monads • Monads define two functions • return takes a

    value and puts it in a box • bind takes a box & function, and returns a box with f(value)
  10. Some math (√4) + 5

  11. Some math (√4) + 5 3 or 7!

  12. Value 4

  13. Monad [4]

  14. return def m_return(x) [x] end # m_r(4) => [4]

  15. Square Root fn def sqrt(x) s = Math.sqrt(x) [s, -s]

    end # sqrt(4) => [2, -2]
  16. Bind Function x = m_return(4) y = ?????(x) { |p|

    sqrt(p) } # I want [-2, 2]
  17. Bind Function x = m_return(4) y = x.flat_map {|p| sqrt(p)

    } # y => [2, -2]
  18. Increment Fn def inc_5(x) [x + 5] end # inc_5(1)

    => [6]
  19. Putting it together m_return(4). flat_map {|p| sqrt(p)}. flat_map {|p| inc_5(p)}

    # => [3, 7]
  20. You have invented the List Monad, used to model non-determinism

    Congrats