leftToMaybe/rightToMaybe

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Wed Aug 7 06:43:08 CEST 2013


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)

>
>
> Erik
> --
> ----------------------------------------------------------------------
> Erik de Castro Lopo
> http://www.mega-nerd.com/
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries



-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
http://IvanMiljenovic.wordpress.com




More information about the Libraries mailing list