mirror of
https://github.com/github/semantic.git
synced 2024-12-24 15:35:14 +03:00
Add maybeM and maybeFail.
`maybeM act may` returns the `Just` in `may` or runs `act`. `maybeFail str may` fails with `str` as an error or extracts the `Just`. These combinators are so useful that I don't know why they're not in the Prelude or some library. I use them constantly.
This commit is contained in:
parent
8701de3af0
commit
1ab4bd0c9d
@ -1,7 +1,9 @@
|
||||
{-# LANGUAGE UndecidableInstances #-}
|
||||
module Prologue (
|
||||
module X
|
||||
, ) where
|
||||
module Prologue
|
||||
( module X
|
||||
, maybeM
|
||||
, maybeFail
|
||||
) where
|
||||
|
||||
|
||||
import Data.Bifunctor.Join as X
|
||||
@ -67,3 +69,11 @@ import Data.Hashable as X (
|
||||
-- Generics
|
||||
import GHC.Generics as X hiding (moduleName)
|
||||
import GHC.Stack as X
|
||||
|
||||
-- Extract the 'Just' of a Maybe in an Applicative context or, given Nothing, run the provided action.
|
||||
maybeM :: Applicative f => f a -> Maybe a -> f a
|
||||
maybeM f = maybe f pure
|
||||
|
||||
-- Either extract the 'Just' of a Maybe or invoke `fail` with the provided string.
|
||||
maybeFail :: MonadFail m => String -> Maybe a -> m a
|
||||
maybeFail s = maybeFail (X.fail s)
|
||||
|
Loading…
Reference in New Issue
Block a user