summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Cacqueray <tdecacqu@redhat.com>2020-03-21 16:55:03 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-03-25 22:42:06 -0400
commit1c5a15f7d257d0a2ded4850e38b6174965a7735b (patch)
treeba37717e836fe681ad8ab9fc3dbcf8e7402ad150
parent7a04920b23376b2759b5049820400ddd40f7b89e (diff)
downloadhaskell-1c5a15f7d257d0a2ded4850e38b6174965a7735b.tar.gz
Base: add Control.Applicative optional example
This change adds an optional example. Tracking: #17929
-rw-r--r--libraries/base/Control/Applicative.hs17
1 files changed, 17 insertions, 0 deletions
diff --git a/libraries/base/Control/Applicative.hs b/libraries/base/Control/Applicative.hs
index 87394e078b..e898bf8c04 100644
--- a/libraries/base/Control/Applicative.hs
+++ b/libraries/base/Control/Applicative.hs
@@ -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