-- | Either operations.

module Data.Either where

import Prelude

-- | Basically forM.
whenLeft :: Either a b -> (a -> Fay c) -> Fay (Maybe c)
whenLeft :: Either a b -> (a -> Fay c) -> Fay (Maybe c)
whenLeft (Left a
x) a -> Fay c
f = a -> Fay c
f a
x Fay c -> Ptr (c -> Fay (Maybe c)) -> Fay (Maybe c)
forall a b. Ptr (Fay a) -> Ptr (a -> Fay b) -> Fay b
>>= Maybe c -> Fay (Maybe c)
forall a. a -> Fay a
return (Maybe c -> Fay (Maybe c))
-> (c -> Maybe c) -> Ptr (c -> Fay (Maybe c))
forall t1 t t2. (t1 -> t) -> (t2 -> t1) -> t2 -> t
. c -> Maybe c
forall a. a -> Maybe a
Just
whenLeft (Right b
_) a -> Fay c
_ = Maybe c -> Fay (Maybe c)
forall a. a -> Fay a
return Maybe c
forall a. Maybe a
Nothing

-- | Basically forM.
whenRight :: Either a b -> (b -> Fay c) -> Fay (Maybe c)
whenRight :: Either a b -> (b -> Fay c) -> Fay (Maybe c)
whenRight (Right b
x) b -> Fay c
f = b -> Fay c
f b
x Fay c -> Ptr (c -> Fay (Maybe c)) -> Fay (Maybe c)
forall a b. Ptr (Fay a) -> Ptr (a -> Fay b) -> Fay b
>>= Maybe c -> Fay (Maybe c)
forall a. a -> Fay a
return (Maybe c -> Fay (Maybe c))
-> (c -> Maybe c) -> Ptr (c -> Fay (Maybe c))
forall t1 t t2. (t1 -> t) -> (t2 -> t1) -> t2 -> t
. c -> Maybe c
forall a. a -> Maybe a
Just
whenRight (Left a
_) b -> Fay c
_ = Maybe c -> Fay (Maybe c)
forall a. a -> Fay a
return Maybe c
forall a. Maybe a
Nothing

-- | Usual isLeft.
isLeft :: Either a b -> Bool
isLeft :: Either a b -> Bool
isLeft (Left a
_) = Bool
True
isLeft Either a b
_ = Bool
False

-- | Usual isRight.
isRight :: Either a b -> Bool
isRight :: Either a b -> Bool
isRight (Right b
_) = Bool
True
isRight Either a b
_ = Bool
False