Skip to content

Error.Class: add convenice lifting functions #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Error.Class: add convenice lifting functions
Add functions that allow `Either` and `Maybe` to be easily lifted to a
MonadThrow monad.
  • Loading branch information
hexagonal-sun committed Jun 17, 2021
commit fa452d46f3e465655317db3633aded41d6961c5f
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