extra-1.8: Extra functions I use.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Either.Extra

Description

This module extends Data.Either with extra operations, particularly to quickly extract from inside an Either. Some of these operations are partial, and should be used with care in production-quality code.

If you need more Either functions see the either.

Synopsis

Documentation

fromLeft :: a -> Either a b -> a #

fromRight :: b -> Either a b -> b #

data Either a b #

Constructors

Left a 
Right b 

Instances

Instances details
Bifoldable Either 
Instance details

Defined in Data.Bifoldable

Methods

bifold :: Monoid m => Either m m -> m

bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Either a b -> m

bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Either a b -> c

bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Either a b -> c

Bifoldable1 Either 
Instance details

Defined in Data.Bifoldable1

Methods

bifold1 :: Semigroup m => Either m m -> m

bifoldMap1 :: Semigroup m => (a -> m) -> (b -> m) -> Either a b -> m

Bifunctor Either 
Instance details

Defined in Data.Bifunctor

Methods

bimap :: (a -> b) -> (c -> d) -> Either a c -> Either b d

first :: (a -> b) -> Either a c -> Either b c

second :: (b -> c) -> Either a b -> Either a c

Bitraversable Either 
Instance details

Defined in Data.Bitraversable

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Either a b -> f (Either c d)

Eq2 Either 
Instance details

Defined in Data.Functor.Classes

Methods

liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> Either a c -> Either b d -> Bool

Ord2 Either 
Instance details

Defined in Data.Functor.Classes

Methods

liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> Either a c -> Either b d -> Ordering

Read2 Either 
Instance details

Defined in Data.Functor.Classes

Methods

liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Either a b)

liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Either a b]

liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (Either a b)

liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [Either a b]

Show2 Either 
Instance details

Defined in Data.Functor.Classes

Methods

liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> Either a b -> ShowS

liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [Either a b] -> ShowS

NFData2 Either 
Instance details

Defined in Control.DeepSeq

Methods

liftRnf2 :: (a -> ()) -> (b -> ()) -> Either a b -> ()

Generic1 (Either a :: Type -> Type) 
Instance details

Defined in GHC.Internal.Generics

Associated Types

type Rep1 (Either a :: Type -> Type) 
Instance details

Defined in GHC.Internal.Generics

type Rep1 (Either a :: Type -> Type) = D1 ('MetaData "Either" "GHC.Internal.Data.Either" "ghc-internal" 'False) (C1 ('MetaCons "Left" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: C1 ('MetaCons "Right" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))

Methods

from1 :: Either a a0 -> Rep1 (Either a) a0

to1 :: Rep1 (Either a) a0 -> Either a a0

(Lift a, Lift b) => Lift (Either a b :: Type) 
Instance details

Defined in Language.Haskell.TH.Syntax

Methods

lift :: Quote m => Either a b -> m Exp

liftTyped :: forall (m :: Type -> Type). Quote m => Either a b -> Code m (Either a b)

Eq a => Eq1 (Either a) 
Instance details

Defined in Data.Functor.Classes

Methods

liftEq :: (a0 -> b -> Bool) -> Either a a0 -> Either a b -> Bool

Ord a => Ord1 (Either a) 
Instance details

Defined in Data.Functor.Classes

Methods

liftCompare :: (a0 -> b -> Ordering) -> Either a a0 -> Either a b -> Ordering

Read a => Read1 (Either a) 
Instance details

Defined in Data.Functor.Classes

Methods

liftReadsPrec :: (Int -> ReadS a0) -> ReadS [a0] -> Int -> ReadS (Either a a0)

liftReadList :: (Int -> ReadS a0) -> ReadS [a0] -> ReadS [Either a a0]

liftReadPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec (Either a a0)

liftReadListPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec [Either a a0]

Show a => Show1 (Either a) 
Instance details

Defined in Data.Functor.Classes

Methods

liftShowsPrec :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> Int -> Either a a0 -> ShowS

liftShowList :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> [Either a a0] -> ShowS

NFData a => NFData1 (Either a) 
Instance details

Defined in Control.DeepSeq

Methods

liftRnf :: (a0 -> ()) -> Either a a0 -> ()

Applicative (Either e) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

pure :: a -> Either e a

(<*>) :: Either e (a -> b) -> Either e a -> Either e b

liftA2 :: (a -> b -> c) -> Either e a -> Either e b -> Either e c

(*>) :: Either e a -> Either e b -> Either e b

(<*) :: Either e a -> Either e b -> Either e a

Functor (Either a) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

fmap :: (a0 -> b) -> Either a a0 -> Either a b #

(<$) :: a0 -> Either a b -> Either a a0 #

Monad (Either e) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

(>>=) :: Either e a -> (a -> Either e b) -> Either e b #

(>>) :: Either e a -> Either e b -> Either e b #

return :: a -> Either e a #

Foldable (Either a) 
Instance details

Defined in GHC.Internal.Data.Foldable

Methods

fold :: Monoid m => Either a m -> m #

foldMap :: Monoid m => (a0 -> m) -> Either a a0 -> m #

foldMap' :: Monoid m => (a0 -> m) -> Either a a0 -> m #

foldr :: (a0 -> b -> b) -> b -> Either a a0 -> b #

foldr' :: (a0 -> b -> b) -> b -> Either a a0 -> b #

foldl :: (b -> a0 -> b) -> b -> Either a a0 -> b #

foldl' :: (b -> a0 -> b) -> b -> Either a a0 -> b #

foldr1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 #

foldl1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 #

toList :: Either a a0 -> [a0] #

null :: Either a a0 -> Bool #

length :: Either a a0 -> Int #

elem :: Eq a0 => a0 -> Either a a0 -> Bool #

maximum :: Ord a0 => Either a a0 -> a0 #

minimum :: Ord a0 => Either a a0 -> a0 #

sum :: Num a0 => Either a a0 -> a0 #

product :: Num a0 => Either a a0 -> a0 #

Traversable (Either a) 
Instance details

Defined in GHC.Internal.Data.Traversable

Methods

traverse :: Applicative f => (a0 -> f b) -> Either a a0 -> f (Either a b)

sequenceA :: Applicative f => Either a (f a0) -> f (Either a a0)

mapM :: Monad m => (a0 -> m b) -> Either a a0 -> m (Either a b) #

sequence :: Monad m => Either a (m a0) -> m (Either a a0) #

(NFData a, NFData b) => NFData (Either a b) 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Either a b -> ()

Semigroup (Either a b) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

(<>) :: Either a b -> Either a b -> Either a b #

sconcat :: NonEmpty (Either a b) -> Either a b

stimes :: Integral b0 => b0 -> Either a b -> Either a b

Generic (Either a b) 
Instance details

Defined in GHC.Internal.Generics

Associated Types

type Rep (Either a b) 
Instance details

Defined in GHC.Internal.Generics

type Rep (Either a b) = D1 ('MetaData "Either" "GHC.Internal.Data.Either" "ghc-internal" 'False) (C1 ('MetaCons "Left" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: C1 ('MetaCons "Right" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 b)))

Methods

from :: Either a b -> Rep (Either a b) x

to :: Rep (Either a b) x -> Either a b

(Read a, Read b) => Read (Either a b) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

readsPrec :: Int -> ReadS (Either a b) #

readList :: ReadS [Either a b] #

readPrec :: ReadPrec (Either a b) #

readListPrec :: ReadPrec [Either a b] #

(Show a, Show b) => Show (Either a b) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

showsPrec :: Int -> Either a b -> ShowS

show :: Either a b -> String

showList :: [Either a b] -> ShowS

(Eq a, Eq b) => Eq (Either a b) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

(==) :: Either a b -> Either a b -> Bool

(/=) :: Either a b -> Either a b -> Bool

(Ord a, Ord b) => Ord (Either a b) 
Instance details

Defined in GHC.Internal.Data.Either

Methods

compare :: Either a b -> Either a b -> Ordering

(<) :: Either a b -> Either a b -> Bool

(<=) :: Either a b -> Either a b -> Bool

(>) :: Either a b -> Either a b -> Bool

(>=) :: Either a b -> Either a b -> Bool

max :: Either a b -> Either a b -> Either a b

min :: Either a b -> Either a b -> Either a b

type Rep1 (Either a :: Type -> Type) 
Instance details

Defined in GHC.Internal.Generics

type Rep1 (Either a :: Type -> Type) = D1 ('MetaData "Either" "GHC.Internal.Data.Either" "ghc-internal" 'False) (C1 ('MetaCons "Left" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: C1 ('MetaCons "Right" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))
type Rep (Either a b) 
Instance details

Defined in GHC.Internal.Generics

type Rep (Either a b) = D1 ('MetaData "Either" "GHC.Internal.Data.Either" "ghc-internal" 'False) (C1 ('MetaCons "Left" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: C1 ('MetaCons "Right" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 b)))

either :: (a -> c) -> (b -> c) -> Either a b -> c #

isLeft :: Either a b -> Bool #

isRight :: Either a b -> Bool #

lefts :: [Either a b] -> [a] #

partitionEithers :: [Either a b] -> ([a], [b]) #

rights :: [Either a b] -> [b] #

fromLeft :: a -> Either a b -> a #

fromRight :: b -> Either a b -> b #

fromEither :: Either a a -> a Source #

Pull the value out of an Either where both alternatives have the same type.

\x -> fromEither (Left x ) == x
\x -> fromEither (Right x) == x

fromLeft' :: Partial => Either l r -> l Source #

The fromLeft' function extracts the element out of a Left and throws an error if its argument is Right. Much like fromJust, using this function in polished code is usually a bad idea.

\x -> fromLeft' (Left  x) == x
\x -> fromLeft' (Right x) == undefined

fromRight' :: Partial => Either l r -> r Source #

The fromRight' function extracts the element out of a Right and throws an error if its argument is Left. Much like fromJust, using this function in polished code is usually a bad idea.

\x -> fromRight' (Right x) == x
\x -> fromRight' (Left  x) == undefined

eitherToMaybe :: Either a b -> Maybe b Source #

Given an Either, convert it to a Maybe, where Left becomes Nothing.

\x -> eitherToMaybe (Left x) == Nothing
\x -> eitherToMaybe (Right x) == Just x

maybeToEither :: a -> Maybe b -> Either a b Source #

Given a Maybe, convert it to an Either, providing a suitable value for the Left should the value be Nothing.

\a b -> maybeToEither a (Just b) == Right b
\a -> maybeToEither a Nothing == Left a

mapLeft :: (a -> c) -> Either a b -> Either c b Source #

The mapLeft function takes a function and applies it to an Either value iff the value takes the form Left _.

mapLeft show (Left 1) == Left "1"
mapLeft show (Right True) == Right True

mapRight :: (b -> c) -> Either a b -> Either a c Source #

The mapRight function takes a function and applies it to an Either value iff the value takes the form Right _.

mapRight show (Left 1) == Left 1
mapRight show (Right True) == Right "True"