{-# LANGUAGE NoImplicitPrelude #-}
module Text.Pandoc.Readers.Odt.Generic.Fallible where
import Prelude
type Failure = ()
type Fallible a = Either Failure a
maybeToEither :: Maybe a -> Fallible a
maybeToEither (Just a) = Right a
maybeToEither Nothing = Left ()
eitherToMaybe :: Either _l a -> Maybe a
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right a) = Just a
fromLeft :: (a -> b) -> Either a b -> b
fromLeft f (Left a) = f a
fromLeft _ (Right b) = b
recover :: a -> Either _f a -> a
recover a (Left _) = a
recover _ (Right a) = a
failWith :: failure -> Either failure _x
failWith f = Left f
failEmpty :: (Monoid failure) => Either failure _x
failEmpty = failWith mempty
succeedWith :: a -> Either _x a
succeedWith = Right
collapseEither :: Either failure (Either failure x)
-> Either failure x
collapseEither (Left f ) = Left f
collapseEither (Right (Left f)) = Left f
collapseEither (Right (Right x)) = Right x
chooseMax :: (Monoid a, Monoid b) => Either a b -> Either a b -> Either a b
chooseMax = chooseMaxWith mappend
chooseMaxWith :: (Monoid a) => (b -> b -> b)
-> Either a b
-> Either a b
-> Either a b
chooseMaxWith (><) (Right a) (Right b) = Right $ a >< b
chooseMaxWith _ (Left a) (Left b) = Left $ a `mappend` b
chooseMaxWith _ (Right a) _ = Right a
chooseMaxWith _ _ (Right b) = Right b
class ChoiceVector v where
spreadChoice :: v (Either f a) -> Either f (v a)
instance ChoiceVector ((,) a) where
spreadChoice (_, Left f) = Left f
spreadChoice (x, Right y) = Right (x,y)
newtype SuccessList a = SuccessList { collectNonFailing :: [a] }
deriving ( Eq, Ord, Show )
instance ChoiceVector SuccessList where
spreadChoice = Right . SuccessList . foldr unTagRight [] . collectNonFailing
where unTagRight (Right x) = (x:)
unTagRight _ = id