From d6a710e40e504260534a095ce187345424c809e8 Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Mon, 16 Apr 2018 14:42:53 +0100 Subject: [PATCH] Expose fromLeft and fromRight --- CHANGES.md | 5 +++++ src/Protolude/Either.hs | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index d16824fd2..aa5cda8f2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,8 @@ +0.2.3 +===== + +* Expose `fromLeft` and `fromRight`. + 0.2.2 ===== diff --git a/src/Protolude/Either.hs b/src/Protolude/Either.hs index 28eab6164..ba3d81dc4 100644 --- a/src/Protolude/Either.hs +++ b/src/Protolude/Either.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE Safe #-} {-# LANGUAGE NoImplicitPrelude #-} @@ -8,12 +9,27 @@ module Protolude.Either ( , rightToMaybe , maybeEmpty , maybeToEither +, fromLeft +, fromRight ) where import Data.Function (const) import Data.Monoid (Monoid, mempty) import Data.Maybe (Maybe(..), maybe) import Data.Either (Either(..), either) +#if MIN_VERSION_base(4,10,0) +import Data.Either (fromLeft, fromRight) +#else +-- | Return the contents of a 'Right'-value or a default value otherwise. +fromLeft :: a -> Either a b -> a +fromLeft _ (Left a) = a +fromLeft a _ = a + +-- | Return the contents of a 'Right'-value or a default value otherwise. +fromRight :: b -> Either a b -> b +fromRight _ (Right b) = b +fromRight b _ = b +#endif leftToMaybe :: Either l r -> Maybe l leftToMaybe = either Just (const Nothing)