Movatterモバイル変換


[0]ホーム

URL:


base-4.12.0.0: Basic libraries

Copyright(c) The University of Glasgow 2001
LicenseBSD-style (see the file libraries/base/LICENSE)
Maintainerlibraries@haskell.org
Stabilityexperimental
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Either

Description

The Either type, and associated operations.

Synopsis

Documentation

dataEither a bSource#

TheEither type represents values with two possibilities: a value oftypeEither a b is eitherLeft a orRight b.

TheEither type is sometimes used to represent a value which iseither correct or an error; by convention, theLeft constructor isused to hold an error value and theRight constructor is used tohold a correct value (mnemonic: "right" also means "correct").

Examples

Expand

The typeEitherStringInt is the type of values which can be eitheraString or anInt. TheLeft constructor can be used only onStrings, and theRight constructor can be used only onInts:

>>>let s = Left "foo" :: Either String Int>>>sLeft "foo">>>let n = Right 3 :: Either String Int>>>nRight 3>>>:type ss :: Either String Int>>>:type nn :: Either String Int

Thefmap from ourFunctor instance will ignoreLeft values, butwill apply the supplied function to values contained in aRight:

>>>let s = Left "foo" :: Either String Int>>>let n = Right 3 :: Either String Int>>>fmap (*2) sLeft "foo">>>fmap (*2) nRight 6

TheMonad instance forEither allows us to chain together multipleactions which may fail, and fail overall if any of the individualsteps failed. First we'll write a function that can either parse anInt from aChar, or fail.

>>>import Data.Char ( digitToInt, isDigit )>>>:{    let parseEither :: Char -> Either String Int        parseEither c          | isDigit c = Right (digitToInt c)          | otherwise = Left "parse error">>>:}

The following should work, since both'1' and'2' can beparsed asInts.

>>>:{    let parseMultiple :: Either String Int        parseMultiple = do          x <- parseEither '1'          y <- parseEither '2'          return (x + y)>>>:}
>>>parseMultipleRight 3

But the following should fail overall, since the first operation wherewe attempt to parse'm' as anInt will fail:

>>>:{    let parseMultiple :: Either String Int        parseMultiple = do          x <- parseEither 'm'          y <- parseEither '2'          return (x + y)>>>:}
>>>parseMultipleLeft "parse error"

Constructors

Left a 
Right b 
Instances
Show2EitherSource#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Methods

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

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

Read2EitherSource#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Methods

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

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

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

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

Ord2EitherSource#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Methods

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

Eq2EitherSource#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Methods

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

BifunctorEitherSource#

Since: 4.8.0.0

Instance details

Defined inData.Bifunctor

Methods

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

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

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

BifoldableEitherSource#

Since: 4.10.0.0

Instance details

Defined inData.Bifoldable

Methods

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

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

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

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

BitraversableEitherSource#

Since: 4.10.0.0

Instance details

Defined inData.Bitraversable

Methods

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

Monad (Either e)Source#

Since: 4.4.0.0

Instance details

Defined inData.Either

Methods

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

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

return :: a ->Either e aSource#

fail ::String ->Either e aSource#

Functor (Either a)Source#

Since: 3.0

Instance details

Defined inData.Either

Methods

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

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

MonadFix (Either e)Source#

Since: 4.3.0.0

Instance details

Defined inControl.Monad.Fix

Methods

mfix :: (a ->Either e a) ->Either e aSource#

Applicative (Either e)Source#

Since: 3.0

Instance details

Defined inData.Either

Methods

pure :: a ->Either e aSource#

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

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

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

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

Foldable (Either a)Source#

Since: 4.7.0.0

Instance details

Defined inData.Foldable

Methods

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

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

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

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

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

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

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

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

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

null ::Either a a0 ->BoolSource#

length ::Either a a0 ->IntSource#

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

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

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

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

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

Traversable (Either a)Source#

Since: 4.7.0.0

Instance details

Defined inData.Traversable

Methods

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

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

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

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

Show a =>Show1 (Either a)Source#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Methods

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

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

Read a =>Read1 (Either a)Source#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Ord a =>Ord1 (Either a)Source#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Methods

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

Eq a =>Eq1 (Either a)Source#

Since: 4.9.0.0

Instance details

Defined inData.Functor.Classes

Methods

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

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

Defined inGHC.Generics

Associated Types

typeRep1 (Either a) :: k ->TypeSource#

Methods

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

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

(Eq a,Eq b) =>Eq (Either a b)Source#

Since: 2.1

Instance details

Defined inData.Either

Methods

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

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

(Data a,Data b) =>Data (Either a b)Source#

Since: 4.0.0.0

Instance details

Defined inData.Data

Methods

gfoldl :: (forall d b0.Data d => c (d -> b0) -> d -> c b0) -> (forall g. g -> c g) ->Either a b -> c (Either a b)Source#

gunfold :: (forall b0 r.Data b0 => c (b0 -> r) -> c r) -> (forall r. r -> c r) ->Constr -> c (Either a b)Source#

toConstr ::Either a b ->ConstrSource#

dataTypeOf ::Either a b ->DataTypeSource#

dataCast1 ::Typeable t => (forall d.Data d => c (t d)) ->Maybe (c (Either a b))Source#

dataCast2 ::Typeable t => (forall d e. (Data d,Data e) => c (t d e)) ->Maybe (c (Either a b))Source#

gmapT :: (forall b0.Data b0 => b0 -> b0) ->Either a b ->Either a bSource#

gmapQl :: (r -> r' -> r) -> r -> (forall d.Data d => d -> r') ->Either a b -> rSource#

gmapQr :: (r' -> r -> r) -> r -> (forall d.Data d => d -> r') ->Either a b -> rSource#

gmapQ :: (forall d.Data d => d -> u) ->Either a b -> [u]Source#

gmapQi ::Int -> (forall d.Data d => d -> u) ->Either a b -> uSource#

gmapM ::Monad m => (forall d.Data d => d -> m d) ->Either a b -> m (Either a b)Source#

gmapMp ::MonadPlus m => (forall d.Data d => d -> m d) ->Either a b -> m (Either a b)Source#

gmapMo ::MonadPlus m => (forall d.Data d => d -> m d) ->Either a b -> m (Either a b)Source#

(Ord a,Ord b) =>Ord (Either a b)Source#

Since: 2.1

Instance details

Defined inData.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#

(Read a,Read b) =>Read (Either a b)Source#

Since: 3.0

Instance details

Defined inData.Either

(Show a,Show b) =>Show (Either a b)Source#

Since: 3.0

Instance details

Defined inData.Either

Generic (Either a b)Source# 
Instance details

Defined inGHC.Generics

Associated Types

typeRep (Either a b) ::Type ->TypeSource#

Methods

from ::Either a b ->Rep (Either a b) xSource#

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

Semigroup (Either a b)Source#

Since: 4.9.0.0

Instance details

Defined inData.Either

Methods

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

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

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

typeRep1 (Either a ::Type ->Type)Source#

Since: 4.6.0.0

Instance details

Defined inGHC.Generics

typeRep (Either a b)Source#

Since: 4.6.0.0

Instance details

Defined inGHC.Generics

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

Case analysis for theEither type. If the value isLeft a, apply the first function toa; if it isRight b, apply the second function tob.

Examples

Expand

We create two values of typeEitherStringInt, one using theLeft constructor and another using theRight constructor. Then we apply "either" thelength function (if we have aString) or the "times-two" function (if we have anInt):

>>>let s = Left "foo" :: Either String Int>>>let n = Right 3 :: Either String Int>>>either length (*2) s3>>>either length (*2) n6

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

Extracts from a list ofEither all theLeft elements. All theLeft elements are extracted in order.

Examples

Expand

Basic usage:

>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>lefts list["foo","bar","baz"]

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

Extracts from a list ofEither all theRight elements. All theRight elements are extracted in order.

Examples

Expand

Basic usage:

>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>rights list[3,7]

isLeft ::Either a b ->BoolSource#

ReturnTrue if the given value is aLeft-value,False otherwise.

Examples

Expand

Basic usage:

>>>isLeft (Left "foo")True>>>isLeft (Right 3)False

Assuming aLeft value signifies some sort of error, we can useisLeft to write a very simple error-reporting function that does absolutely nothing in the case of success, and outputs "ERROR" if any error occurred.

This example shows howisLeft might be used to avoid pattern matching when one does not care about the value contained in the constructor:

>>>import Control.Monad ( when )>>>let report e = when (isLeft e) $ putStrLn "ERROR">>>report (Right 1)>>>report (Left "parse error")ERROR

Since: 4.7.0.0

isRight ::Either a b ->BoolSource#

ReturnTrue if the given value is aRight-value,False otherwise.

Examples

Expand

Basic usage:

>>>isRight (Left "foo")False>>>isRight (Right 3)True

Assuming aLeft value signifies some sort of error, we can useisRight to write a very simple reporting function that only outputs "SUCCESS" when a computation has succeeded.

This example shows howisRight might be used to avoid pattern matching when one does not care about the value contained in the constructor:

>>>import Control.Monad ( when )>>>let report e = when (isRight e) $ putStrLn "SUCCESS">>>report (Left "parse error")>>>report (Right 1)SUCCESS

Since: 4.7.0.0

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

Return the contents of aLeft-value or a default value otherwise.

Examples

Expand

Basic usage:

>>>fromLeft 1 (Left 3)3>>>fromLeft 1 (Right "foo")1

Since: 4.10.0.0

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

Return the contents of aRight-value or a default value otherwise.

Examples

Expand

Basic usage:

>>>fromRight 1 (Right 3)3>>>fromRight 1 (Left "foo")1

Since: 4.10.0.0

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

Partitions a list ofEither into two lists. All theLeft elements are extracted, in order, to the first component of the output. Similarly theRight elements are extracted to the second component of the output.

Examples

Expand

Basic usage:

>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list(["foo","bar","baz"],[3,7])

The pair returned bypartitionEithers x should be the same pair as(lefts x,rights x):

>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list == (lefts list, rights list)True

Produced byHaddock version 2.20.0


[8]ページ先頭

©2009-2025 Movatter.jp