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

Monads 101 with C++

Monads 101 with C++

Basics about functors and monads and implementation examples using modern C++

Avatar for Denis Kormalev

Denis Kormalev

December 18, 2018
Tweet

More Decks by Denis Kormalev

Other Decks in Programming

Transcript

  1. Monad operations • Type constructor • unit • flatMap (or

    bind in Haskell) • Monad is usually a functor
  2. Monad laws • Left identity: • Monad.unit(x).flatMap(A) === A(x) •

    Right identity: • x.flatMap(Monad.unit) === x • Associativity: • x.flatMap(A).flatMap(B) === x.flatMap(xx -> A(xx).flatMap(B))
  3. Other useful monads • Eval • Delays calculation and trampolines

    its flatMaps • Future • Represents value that will be accessible at some future point of timeline • Reader • Carries value through morphisms (e.g. configuration) • Writer • Returns some additional data with the answer (e.g. logging) • State • Combination of Reader and Writer, carries data through morphisms and allows to change it
  4. Other useful morphisms at OptionT • Option<T> -> Option<NewT> •

    Option<T> -> M<Option<NewT>> • M<?> -> NewM<?>