isLeft/isRight (Was: leftToMaybe/rightToMaybe)
Henning Thielemann
lemming at henning-thielemann.de
Wed Aug 7 10:26:03 CEST 2013
On Wed, 7 Aug 2013, Erik de Castro Lopo wrote:
> In the second link above you wrote:
I have included both links since the discussion in November continued in
December.
>> It would be a good opportunity to check how these packages use isLeft and
>> isRight. E.g. if they use them in connection with fromLeft and fromRight
>> then this would be an argument for me to exclude isLeft and isRight as
>> well.
>
> My main usage of isLeft/isRight is in HSpec tests where I write:
>
> value1 `shouldSatisfy' isLeft
> value2 `shouldSatisfy' isRight
Yes, from what I have seen in some packages, testing seems to be the most
sensible usage of 'isLeft' and 'isRight'. In other contexts 'isLeft' is
usually combined with a non-total 'fromLeft', which is certainly a bad
idea.
> I would also be opposed to a fromLeft defined anything like fromJust:
>
> Data.Maybe.fromJust :: Maybe a -> a
>
> but would not be opposed to a fromLeft defined as John Wiegley suggested:
>
> fromLeft :: a -> Either a b -> a
> fromLeft _ (Left x) = x
> fromLeft x _ = x
It looks indeed better than the non-total fromLeft. On the other hand the
name would be inconsistent with fromJust, and the total fromLeft could be
written as:
fromLeft x = either (const x) id
or using the proposed maybeLeft as
fromLeft x = fromMaybe x . maybeLeft
I don't know how often you need fromLeft.
More information about the Libraries
mailing list