module Data.Either.Combinators
( isLeft
, isRight
, fromLeft
, fromRight
, fromLeft'
, fromRight'
, mapBoth
, mapLeft
, mapRight
, whenLeft
, whenRight
, unlessLeft
, unlessRight
, leftToMaybe
, rightToMaybe
) where
import Control.Applicative
isLeft :: Either a b -> Bool
isLeft (Left _) = True
isLeft _ = False
isRight :: Either a b -> Bool
isRight (Right _) = True
isRight _ = False
fromLeft' :: Either a b -> a
fromLeft' (Right _) = error "Data.Either.Combinators.fromLeft: Argument takes form 'Right _'"
fromLeft' (Left x) = x
fromRight' :: Either a b -> b
fromRight' (Left _) = error "Data.Either.Combinators.fromRight: Argument takes form 'Left _'"
fromRight' (Right x) = x
mapBoth :: (a -> c) -> (b -> d) -> Either a b -> Either c d
mapBoth f _ (Left x) = Left (f x)
mapBoth _ f (Right x) = Right (f x)
mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft f = mapBoth f id
mapRight :: (b -> c) -> Either a b -> Either a c
mapRight = mapBoth id
whenLeft :: Applicative m => Either a b -> (a -> m ()) -> m ()
whenLeft (Left x) f = f x
whenLeft _ _ = pure ()
whenRight :: Applicative m => Either a b -> (b -> m ()) -> m ()
whenRight (Right x) f = f x
whenRight _ _ = pure ()
unlessLeft :: Applicative m => Either a b -> (b -> m ()) -> m ()
unlessLeft = whenRight
unlessRight :: Applicative m => Either a b -> (a -> m ()) -> m ()
unlessRight = whenLeft
fromLeft :: a -> Either a b -> a
fromLeft _ (Left x) = x
fromLeft x _ = x
fromRight :: b -> Either a b -> b
fromRight _ (Right x) = x
fromRight x _ = x
leftToMaybe :: Either a b -> Maybe a
leftToMaybe = either Just (const Nothing)
rightToMaybe :: Either a b -> Maybe b
rightToMaybe = either (const Nothing) Just