Skip to content

Commit

Permalink
Error.Class: add convenice lifting functions (#140)
Browse files Browse the repository at this point in the history
Add functions that allow `Either` and `Maybe` to be easily lifted to a
MonadThrow monad.
  • Loading branch information
hexagonal-sun authored Jul 11, 2021
1 parent 60ed3f6 commit 254c3f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Add `liftMaybe` and `liftEither` to easily lift `Maybe` and `Either` values
to a `MonadThrow` monad.

Bugfixes:

Expand Down
10 changes: 9 additions & 1 deletion src/Control/Monad/Error/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Control.Monad.Error.Class where

import Prelude

import Data.Maybe (Maybe(..))
import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..), maybe)
import Effect (Effect)
import Effect.Exception as Ex

Expand Down Expand Up @@ -102,3 +102,11 @@ withResource acquire release kleisli = do
result <- try $ kleisli resource
release resource
either throwError pure result

-- | Lift a `Maybe` value to a MonadThrow monad.
liftMaybe :: forall m e a. MonadThrow e m => e -> Maybe a -> m a
liftMaybe error = maybe (throwError error) pure

-- | Lift an `Either` value to a MonadThrow monad.
liftEither :: forall m e a. MonadThrow e m => Either e a -> m a
liftEither = either throwError pure

0 comments on commit 254c3f2

Please sign in to comment.