by eklerks · 14y ago · 0 comments
Say we have a failing computation: failComp = Nothing
and a couple of succeeding computations: sucComp = Just 1 sucComp2 = Just 2
We can use the typeclasses to avoid explicit unpacking:
-- Monad result: Just 3
add = do x <- sucComp2 y <- sucComp return $ x + y
Applicative result: Just 3
-- the <#> should be the applicative operator. addapp = (+) <$> sucComp <#> sucComp2
Alternative: result (Just 1)
val = failComp <|> sucComp
Monoid: result (Just 2)
mon = failComp <> sucComp2
Functor result (Just 6)
-- # should be multiply operator
func = fmap (#3) sucComp2