6. 6
World full of failing cases (PROBLEM)
Verify Monad propagates error and composes
2 Programming Paradigms
Deducing answer from Monad
Coding the Monad
TABLE OF CONTENTS
10. 10
A simple game: MAD.A.DAM
1. M: Multiply input to a random Int
2. A: Add input to a random Int
3. D: Divide input by a random Int
4. Assume Swift doesn’t have Optional
Perform: MAD.A.DAM on a dice roll input.
29. 29
RESULT MONAD
T
map
flatMap
T -> U U
T -> U U
Monad is a Contextual
Type that provides
linear binding and
composition along
with error
propagation.
30. Monad is a CONTEXTUAL TYPE that provides easy
BINDING/COMPOSITION without sacrificing Error Handling.
44. 44
Promise / Future / Deferred / IO :: Monad
aTask: ( ( (T) -> Void)? -> Void )?
aTask
T
then/mapT -> U aTaskaTask
T
U
T
->
U
45. 45
Promise / Future / Deferred / IO :: Monad
aTask: ( ( (T) -> Void)? -> Void )?
aTask
T
bind/ flatMapT -> Promise<U> aTaskaTask
T
U
T
->
U
46. 46
Promise / Future / Deferred / IO :: Monad
aTaskaTask
T
U
Evaluate and finally call into outer (U -> Void)
By intending to evaluate the inner
block and calling into outer block with
eventual value; we unpack a promise.
None of the Pomise are evaluated
unless someone calls `execute()` on
the outermost layer.
Thereby guarantee of order of
execution is applied.
54. 54
A good programmer is
someone who looks both
ways before crossing a
one-way street.” — Doug
Linder
55. 55
References
1. Monad Research Paper
http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baast
ad.pdf
2. Railway Oriented Programming
https://fsharpforfunandprofit.com/rop/
3. Dr. Bartosz Category theory lecture
https://www.youtube.com/user/DrBartosz/videos
4. My blog for more detailed explanation
https://kandelvijaya.com/2017/08/13/promisein30lines/
5. Promise<T> from the talk
https://github.com/kandelvijaya/MPromise
6. Result<T> from the talk
https://github.com/kandelvijaya/ResultType
56. This presentation and its contents are strictly confidential. It may not, in
whole or in part, be reproduced, redistributed, published or passed on to
any other person by the recipient.
The information in this presentation has not been independently verified. No
representation or warranty, express or implied, is made as to the accuracy
or completeness of the presentation and the information contained herein
and no reliance should be placed on such information. No responsibility is
accepted for any liability for any loss howsoever arising, directly or
indirectly, from this presentation or its contents.
DISCLAIMER
56