module Data.Either.Extras
  ( unsafeFromEither
  , fromRightM
  ) where

import Control.Exception

{-# INLINE unsafeFromEither #-}
-- | If argument is `Left` throw an exception, otherwise return the value inside `Right`.
unsafeFromEither :: Exception e => Either e a -> a
unsafeFromEither :: forall e a. Exception e => Either e a -> a
unsafeFromEither = (e -> a) -> (a -> a) -> Either e a -> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either e -> a
forall a e. Exception e => e -> a
throw a -> a
forall a. a -> a
id

{-# INLINE fromRightM #-}
-- | If argument is `Right` return its value,
-- otherwise apply the given action to the value of Left and return its result.
fromRightM :: Monad m => (a -> m b) -> Either a b -> m b
fromRightM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Either a b -> m b
fromRightM a -> m b
f = (a -> m b) -> (b -> m b) -> Either a b -> m b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> m b
f b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure