Data/Either.hs (original) (raw)

#ifdef GLASGOW_HASKELL

#endif

module Data.Either ( Either(..), either,
lefts,
rights,
partitionEithers, ) where

#include "Typeable.h"

#ifdef GLASGOW_HASKELL import GHC.Base import GHC.Show import GHC.Read #endif

import Data.Typeable

#ifdef GLASGOW_HASKELL

data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)

instance Functor (Either a) where fmap _ (Left x) = Left x fmap f (Right y) = Right (f y)

instance Monad (Either e) where return = Right Left l >>= _ = Left l Right r >>= k = k r

either :: (a -> c) -> (b -> c) -> Either a b -> c either f _ (Left x) = f x either _ g (Right y) = g y #endif /* GLASGOW_HASKELL */

INSTANCE_TYPEABLE2(Either,eitherTc,"Either")

lefts :: [Either a b] -> [a] lefts x = [a | Left a <- x]

rights :: [Either a b] -> [b] rights x = [a | Right a <- x]

partitionEithers :: [Either a b] -> ([a],[b]) partitionEithers = foldr (either left right) ([],[]) where left a ~(l, r) = (a:l, r) right a ~(l, r) = (l, a:r)