{-# LANGUAGE CPP, ConstraintKinds #-}
{-# OPTIONS_GHC -fno-warn-duplicate-exports #-}
module Data.Either.Extra(
module Data.Either,
fromLeft, fromRight, fromEither,
fromLeft', fromRight',
eitherToMaybe, maybeToEither,
mapLeft, mapRight,
) where
import Data.Either
import Partial
#if __GLASGOW_HASKELL__ < 801
fromLeft :: a -> Either a b -> a
fromLeft _ (Left a) = a
fromLeft a _ = a
fromRight :: b -> Either a b -> b
fromRight _ (Right b) = b
fromRight b _ = b
#endif
fromLeft' :: Partial => Either l r -> l
fromLeft' :: Either l r -> l
fromLeft' (Left l
x) = l
x
fromLeft' Either l r
_ = [Char] -> l
forall a. HasCallStack => [Char] -> a
error [Char]
"fromLeft', given a Right"
fromRight' :: Partial => Either l r -> r
fromRight' :: Either l r -> r
fromRight' (Right r
x) = r
x
fromRight' Either l r
_ = [Char] -> r
forall a. HasCallStack => [Char] -> a
error [Char]
"fromRight', given a Left"
fromEither :: Either a a -> a
fromEither :: Either a a -> a
fromEither = (a -> a) -> (a -> a) -> Either a a -> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> a
forall a. a -> a
id a -> a
forall a. a -> a
id
maybeToEither :: a -> Maybe b -> Either a b
maybeToEither :: a -> Maybe b -> Either a b
maybeToEither a
a (Just b
b) = b -> Either a b
forall a b. b -> Either a b
Right b
b
maybeToEither a
a Maybe b
Nothing = a -> Either a b
forall a b. a -> Either a b
Left a
a
eitherToMaybe :: Either a b -> Maybe b
eitherToMaybe :: Either a b -> Maybe b
eitherToMaybe = (a -> Maybe b) -> (b -> Maybe b) -> Either a b -> Maybe b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe b -> a -> Maybe b
forall a b. a -> b -> a
const Maybe b
forall a. Maybe a
Nothing) b -> Maybe b
forall a. a -> Maybe a
Just
mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft a -> c
f = (a -> Either c b) -> (b -> Either c b) -> Either a b -> Either c b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (c -> Either c b
forall a b. a -> Either a b
Left (c -> Either c b) -> (a -> c) -> a -> Either c b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> c
f) b -> Either c b
forall a b. b -> Either a b
Right
mapRight :: (b -> c) -> Either a b -> Either a c
mapRight :: (b -> c) -> Either a b -> Either a c
mapRight = (b -> c) -> Either a b -> Either a c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap