| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Precursor.Data.Either
Documentation
data Either a b :: * -> * -> * #
The Either type represents values with two possibilities: a value of
type is either Either a b or Left a.Right b
The Either type is sometimes used to represent a value which is
either correct or an error; by convention, the Left constructor is
used to hold an error value and the Right constructor is used to
hold a correct value (mnemonic: "right" also means "correct").
Examples
The type is the type of values which can be either
a Either String IntString or an Int. The Left constructor can be used only on
Strings, and the Right constructor can be used only on Ints:
>>>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
The fmap from our Functor instance will ignore Left values, but
will apply the supplied function to values contained in a Right:
>>>let s = Left "foo" :: Either String Int>>>let n = Right 3 :: Either String Int>>>fmap (*2) sLeft "foo">>>fmap (*2) nRight 6
The Monad instance for Either allows us to chain together multiple
actions which may fail, and fail overall if any of the individual
steps failed. First we'll write a function that can either parse an
Int from a Char, 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 be
parsed as Ints.
>>>:{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 where
we attempt to parse 'm' as an Int will fail:
>>>:{let parseMultiple :: Either String Int parseMultiple = do x <- parseEither 'm' y <- parseEither '2' return (x + y)>>>:}
>>>parseMultipleLeft "parse error"
Instances
| Eq2 Either | |
| Ord2 Either | |
| Read2 Either | |
| Show2 Either | |
| Bifunctor Either | |
| Monad (Either e) | |
| Functor (Either a) | |
| MonadFix (Either e) | |
| Applicative (Either e) | |
| Foldable (Either a) | |
| Traversable (Either a) | |
| Generic1 (Either a) | |
| Eq a => Eq1 (Either a) | |
| Ord a => Ord1 (Either a) | |
| Read a => Read1 (Either a) | |
| Show a => Show1 (Either a) | |
| (Eq b, Eq a) => Eq (Either a b) | |
| (Ord b, Ord a) => Ord (Either a b) | |
| (Read b, Read a) => Read (Either a b) | |
| (Show b, Show a) => Show (Either a b) | |
| Generic (Either a b) | |
| Semigroup (Either a b) | |
| (Lift a, Lift b) => Lift (Either a b) | |
| type Rep1 (Either a) | |
| type Rep (Either a b) | |
| type (==) (Either k k1) a b | |
partitionEithers :: [Either a b] -> ([a], [b]) #
Partitions a list of Either into two lists.
All the Left elements are extracted, in order, to the first
component of the output. Similarly the Right elements are extracted
to the second component of the output.
Examples
Basic usage:
>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list(["foo","bar","baz"],[3,7])
The pair returned by should be the same
pair as partitionEithers x(:lefts x, rights x)
>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list == (lefts list, rights list)True