Commit 1c5a15f7 authored by Tristan Cacqueray's avatar Tristan Cacqueray Committed by Marge Bot

Base: add Control.Applicative optional example

This change adds an optional example.

Tracking: #17929
parent 7a04920b
......@@ -141,5 +141,22 @@ instance Alternative ZipList where
-- extra functions
-- | One or none.
--
-- It is useful for modelling any computation that is allowed to fail.
--
-- ==== __Examples__
--
-- Using the Control.Monad.Except as an Alternative, a couple of functions:
--
-- >>> canFail = throwError "it failed" :: Except String Int
-- >>> final = return 42 :: Except String Int
--
-- Can be combined by allowing the first function to fail:
--
-- >>> runExcept $ canFail *> final
-- Left "it failed"
-- >>> runExcept $ optional canFail *> final
-- Right 42
optional :: Alternative f => f a -> f (Maybe a)
optional v = Just <$> v <|> pure Nothing
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment