leftToMaybe/rightToMaybe

Erik de Castro Lopo mle+hs at mega-nerd.com
Wed Aug 7 06:56:53 CEST 2013


Ivan Lazar Miljenovic wrote:

> On 7 August 2013 14:06, Erik de Castro Lopo <mle+hs at mega-nerd.com> wrote:
> > Christopher Done wrote:
> >
> >> How about popping these in Data.Either?
> >>
> >> -- | Maybe get the left side of an Either.
> >> leftToMaybe :: Either a b -> Maybe a
> >> leftToMaybe = either Just (const Nothing)
> >>
> >> -- | Maybe get the right side of an Either.
> >> rightToMaybe :: Either a b -> Maybe b
> >> rightToMaybe = either (const Nothing) Just
> >
> > +1
> >
> > And while we're at it:
> >
> > isLeft :: Either a b -> Bool
> > isLeft (Left _) = True
> > isLeft (Right _) = False
> >
> > isRight :: Either a b -> Bool
> > isRight (Left _) = False
> > isRight (Right _) = True
> 
> If we're starting to add functions to Data.Either, I've often used
> something like this:
> 
> mapEither :: (l -> l') -> (r -> r') -> Either l r -> Either l' r'
> mapEither fl fr = either (Left . fl) (Right . fr)

+1

Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/




More information about the Libraries mailing list