Last active
March 12, 2017 00:32
-
-
Save michelrandahl/2008dd709f29cc9a53def5edc3f82852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Defining some Maybe values... | |
-- These could just as well have been database lookups or something else | |
x : Maybe Nat | |
x = Just 42 | |
y : Maybe String | |
y = Just "hello idris" | |
z : Maybe Nat | |
z = Just 4 | |
-- we wish to sum the above values, -but only if all of them exist... | |
-- we might start out doing it this way.. Typing match case stuff until our fingers fall off.. | |
addStuffBad : Maybe Nat | |
addStuffBad = | |
case x of | |
Nothing => Nothing | |
(Just x') => | |
case y of | |
Nothing => Nothing | |
(Just y') => | |
case z of | |
Nothing => Nothing | |
(Just z') => Just (x' + length y' + z') | |
-- then we discover the allmighty Bind function.. and carefully count parentheses | |
addStuffBetter : Maybe Nat | |
addStuffBetter = | |
x >>= (\x' => | |
y >>= (\y' => | |
z >>= (\z' => | |
Just (x' + length y' + z')))) | |
-- finaly we learn about the existence of the neat do syntax... syntax sugar!, yummy! | |
addStuffNeat : Maybe Nat | |
addStuffNeat = | |
do x' <- x | |
y' <- y | |
z' <- z | |
pure (x' + length y' + z') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment