3 ms·
One example would be to simulate some form of early return from a procedural language. Suppose you have a function which executes some operations, and these ope
by assbuttbuttass 3y ago
One example would be to simulate some form of early return from a procedural language. Suppose you have a function which executes some operations, and these operations can sometimes fail, so they return a Maybe (probably Elixer has some form of optional type)
You could write this without making use of Monads using:
f = case op1 of
Nothing -> Nothing
Just x ->
case op2 x of
Nothing -> Nothing
Just y ->
case op3 y of
...
Or you can use the Maybe monad to make this a bit nicer
f = op1 >>= (\x ->
op2 x >>= (\y ->
op3 y >>= (\z -> ...)))
Or you would probably use the do notation, which is just shorthand for >>=
f = do x <- op1
y <- op2 x
z <- op3 y
...