module Agda.Utils.Either where
whileLeft :: Monad m => (a -> Either b c) -> (a -> b -> m a) -> (a -> c -> m d) -> a -> m d
whileLeft test left right = loop where
loop a =
case test a of
Left b -> loop =<< left a b
Right c -> right a c
caseEitherM :: Monad m => m (Either a b) -> (a -> m c) -> (b -> m c) -> m c
caseEitherM mm f g = either f g =<< mm
mapEither :: (a -> c) -> (b -> d) -> Either a b -> Either c d
mapEither f g = either (Left . f) (Right . g)
mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft f = mapEither f id
mapRight :: (b -> d) -> Either a b -> Either a d
mapRight = mapEither id
traverseEither :: Functor f => (a -> f c) -> (b -> f d) -> Either a b -> f (Either c d)
traverseEither f g = either (fmap Left . f) (fmap Right . g)
isRight :: Either a b -> Bool
isRight (Right _) = True
isRight (Left _) = False
isLeft :: Either a b -> Bool
isLeft (Right _) = False
isLeft (Left _) = True
fromLeft :: (b -> a) -> Either a b -> a
fromLeft = either id
fromRight :: (a -> b) -> Either a b -> b
fromRight f = either f id
fromLeftM :: Monad m => (b -> m a) -> m (Either a b) -> m a
fromLeftM f m = either return f =<< m
fromRightM :: Monad m => (a -> m b) -> m (Either a b) -> m b
fromRightM f m = either f return =<< m
maybeLeft :: Either a b -> Maybe a
maybeLeft = either Just (const Nothing)
maybeRight :: Either a b -> Maybe b
maybeRight = either (const Nothing) Just
allLeft :: [Either a b] -> Maybe [a]
allLeft = mapM maybeLeft
allRight :: [Either a b] -> Maybe [b]
allRight = mapM maybeRight
maybeToEither :: Maybe a -> Either () a
maybeToEither = maybe (Left ()) Right