Skip to content

Commit 804fc73

Browse files
ADD: optional definition.
1 parent 435566d commit 804fc73

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Data/Maybe.purs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Data.Maybe where
22

33
import Prelude
44

5-
import Control.Alt (class Alt)
5+
import Control.Alt (class Alt, (<|>))
66
import Control.Alternative (class Alternative)
77
import Control.Extend (class Extend)
88
import Control.MonadZero (class MonadZero)
@@ -269,3 +269,12 @@ isNothing = maybe true (const false)
269269
-- | runtime.
270270
fromJust :: forall a. Partial => Maybe a -> a
271271
fromJust (Just x) = x
272+
273+
-- | One or none.
274+
-- |
275+
-- | ``` purescript
276+
-- | optional empty = pure Nothing
277+
-- | optional (pure x) = pure (Just x)
278+
-- | ```
279+
optional :: forall f a. Alternative f => f a -> f (Maybe a)
280+
optional a = map Just a <|> pure Nothing

0 commit comments

Comments
 (0)