Skip to content

Commit cf33800

Browse files
authored
Document the relationship of optional to the left catch law (#52)
1 parent 4f1882d commit cf33800

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/Data/Maybe.purs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,23 @@ fromJust (Just x) = x
280280

281281
-- | One or none.
282282
-- |
283-
-- | ``` purescript
283+
-- | ```purescript
284284
-- | optional empty = pure Nothing
285-
-- | optional (pure x) = pure (Just x)
285+
-- | ```
286+
-- |
287+
-- | The behaviour of `optional (pure x)` depends on whether the `Alt` instance
288+
-- | satisfy the left catch law (`pure a <|> b = pure a`).
289+
-- |
290+
-- | `Either e` does:
291+
-- |
292+
-- | ```purescript
293+
-- | optional (Right x) = Right (Just x)
294+
-- | ```
295+
-- |
296+
-- | But `Array` does not:
297+
-- |
298+
-- | ```purescript
299+
-- | optional [x] = [Just x, Nothing]
286300
-- | ```
287301
optional :: forall f a. Alt f => Applicative f => f a -> f (Maybe a)
288302
optional a = map Just a <|> pure Nothing

0 commit comments

Comments
 (0)