Monads in Practice 
Christophe Marchal | Software Architect
A couple of links 
christophe.marchal@ilegra.com 
@toff63 
http://github.com/toff63 
http://francesbagual.net
Disclaimer
Manage complexity with Monad
Study Monads online is hard
Academic talks 
Functor 
Monoids 
Monads 
Category 
theory
Category Theory
Function 
Monoids 
Associative 
( 1 + 2 ) + 3 = 1 + ( 2 + 3 ) 
InitiAl Value 
Result stay in the same 
category
Monoid Example
Monoid
Functor 
Things that can be map over 
capacity BEHaviour 
map
Functor: Example
Functor: Example
Monads
Problem it solves
Scenario 
F 1 F 2
sweet 
Monads Rules: function to create it
Broken Composition 
F 1 
F 2 sweet
Monads Rules: You need a Functor 
F 1 
map ( f 2 ) 
sweet
Scenario 
F 1 F 3 
sweet
Problem: Nested Monad 
F 1 
sweet 
map ( f 3 ) 
sweet
F 1 
sweet 
Flatmap ( f 3 ) 
Monads Rules: You need a Flatmap
Business rule 
TRUE False 
age > 18 
Authorized Not 
Authorized
Business rule implementation
What we want
Monad (?) Code
Trade-off 
More code than before 
Better API 
Easy to reuse 
Potential complexity Managed
Traditional Code
Side Effect 
Our method can return either a sequence 
either null
Exist 
? 
TRUE False 
null ? 
Riendutout quelquechose
Encapsulating if null with Option
Creating our OptionBagual
Code with Options
Business code
Side Effect 
Our method can return either an option 
[sequence] either throw an exception
Try! 
Success Failure 
Try to 
execute 
Gremio Inter
Encapsulating try/catch with Try
Encapsulating try/catch with Try
Code with Try
Business code
Connection Handling
No silver bullet
Loan Pattern
Final Code
Conclusion 
● NO IFs 
● Border cases handled on demand 
● More declarative types 
● Highlight business code 
● Easy to create 
● More reusable code
Thank You! 
Christophe Marchal | Software Architect

Monads in practice