Skip to content

Instantly share code, notes, and snippets.

View teivah's full-sized avatar
🧮
Current status: teivah/majorana

Teiva Harsanyi teivah

🧮
Current status: teivah/majorana
View GitHub Profile
withApplicative :: Int -> String -> Maybe String
withApplicative age name = greet <$> (Just name) <*> validateAge age
validateAge :: Int -> Maybe Int
validateAge age
| age >= 18 = return age
| otherwise = Nothing
greet :: String -> String
greet name = "Hello " ++ name
divide :: Float -> Float -> Maybe Float
divide _ 0 = Nothing
divide x y = return (x / y)
alt2 :: Int -> Maybe Int
alt2 41 = Nothing
alt2 x = return (x + 1)
applicativeEx :: Maybe Int
applicativeEx = (+ 1) <*> Just 42 -- Just 43
fmapEx :: Maybe Int
fmapEx = fmap (Just (+ 1)) (Just 42) -- Does not compile
fmapEx :: [Int] -- A function returning a list
fmapEx = fmap (+ 1) [1, 2, 3] -- [2, 3, 4]
fmapEx :: Maybe Int
fmapEx = fmap (+ 1) Nothing -- Nothing
applicativeEx :: Maybe Int
applicativeEx = (+ 1) <$> Just 42 -- Just 43
applicativeEx :: [Int]
applicativeEx = [(+ 1)] <*> [1, 2, 3] -- [2, 3, 4]