TITLE IN CAPITAL
LETTERS
VIJAYA P. KANDEL
@kandelvijaya
iOS Engineer
Why Monadic computation?
2
3
When does it end? 1 more feature
4
Spot a problem!
5
Problems
1. Composability
2. Error propagation
3. True Type signature
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
7
Are you connected?
Is Wifi working?
A world of 2 possibilities.
8
How do we handle failable
computation?
9
The two halves
Imperative
Programming
Functional
Programming
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.
11
A simple game: MAD.A.DAM
12
A simple game: MAD.A.DAM
13
A simple game: MAD.A.DAM
14
A simple game: MAD.A.DAM
15
A simple game: MAD.A.DAM
16
A simple game: MAD.A.DAM
17
The true division
18
Contextual Box
T Error
Result<T> Contextual Box
19
Contextual Box
Result<Int>Div(a:)Int
Mul(a:)Int Int
20
MAD.A.DAM
21
MAD.A.DAM
22
MAD.A.DAM
23
MAD.A.DAM
24
MAD.A.DAM
25
T
Error
T
flatten()
26
FLATTEN
27
MAD.A.DAM
28
RESULT MONAD
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.
Monad is a CONTEXTUAL TYPE that provides easy
BINDING/COMPOSITION without sacrificing Error Handling.
31
RESULT MONAD
T
map
flatMap
T -> U U
T -> U U
Optional<T> is a
monad. That is why we
can chain stuff
together.
- map()
- flatMap()
32
RESULT MONAD
T
map
flatMap
T -> U U
T -> U U
Collection<T> is a
monad.
- map()
- flatMap()
33
RESULT MONAD
T
map
flatMap
T -> U U
T -> U U
Swift Try/Catch in
principle can be
implemented in terms
of a Result Monad.
34
MAD.A.DAM
35
VERIFICATION
36
Verification: True Types
37
Verification: Error Propagation
38
Verification: Error Propagation
39
Verification: Composability
Who cares error
handling Solution!
I Do! Monadic Solution!
40
WAIT, What about the async
task that might fail?
41
Async Monad
42
Async Monad
43
Promise / Future / Deferred / IO :: Monad
aTask: ( ( (T) -> Void)? -> Void )?
T
aTask
T
44
Promise / Future / Deferred / IO :: Monad
aTask: ( ( (T) -> Void)? -> Void )?
aTask
T
then/mapT -> U aTaskaTask
T
U
T
->
U
45
Promise / Future / Deferred / IO :: Monad
aTask: ( ( (T) -> Void)? -> Void )?
aTask
T
bind/ flatMapT -> Promise<U> aTaskaTask
T
U
T
->
U
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.
47
Async Monad: USAGE
48
Async Monad: USAGE
49
Async Monad: USAGE
50
Async Monad: USAGE
51
Async Monad: USAGE
52
Async Monad: USAGE
53
Async Monad: USAGE
54
A good programmer is
someone who looks both
ways before crossing a
one-way street.” — Doug
Linder
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
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
Vijaya P. Kandel
vijaya.prakash.kandel@zalando.de
@kandelvijaya
20-11-2017
Fashion Store
iOS Engineer

Monadic Computation in Swift. Why? UA Mobile 2017.