[commit: ghc] master: Data.Either: Add fromLeft and fromRight (#12402) (a0f83a6)

git at git.haskell.org git at git.haskell.org
Wed Jul 20 13:18:16 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/a0f83a628cc6a00f948662f88e711c2a37bfda60/ghc

>---------------------------------------------------------------

commit a0f83a628cc6a00f948662f88e711c2a37bfda60
Author: Dylan Meysmans <contact at mettekou.com>
Date:   Wed Jul 20 09:54:55 2016 +0200

    Data.Either: Add fromLeft and fromRight (#12402)
    
    Reviewers: austin, hvr, RyanGlScott, bgamari
    
    Reviewed By: RyanGlScott, bgamari
    
    Subscribers: RyanGlScott, thomie
    
    Differential Revision: https://phabricator.haskell.org/D2403
    
    GHC Trac Issues: #12402


>---------------------------------------------------------------

a0f83a628cc6a00f948662f88e711c2a37bfda60
 docs/users_guide/8.2.1-notes.rst |  2 ++
 libraries/base/Data/Either.hs    | 36 ++++++++++++++++++++++++++++++++++++
 libraries/base/changelog.md      |  2 ++
 3 files changed, 40 insertions(+)

diff --git a/docs/users_guide/8.2.1-notes.rst b/docs/users_guide/8.2.1-notes.rst
index 5f45bf1..27b49ef 100644
--- a/docs/users_guide/8.2.1-notes.rst
+++ b/docs/users_guide/8.2.1-notes.rst
@@ -98,6 +98,8 @@ See ``changelog.md`` in the ``base`` package for full release notes.
 
 -  Version number 4.10.0.0 (was 4.9.0.0)
 
+- ``Data.Either`` now provides ``fromLeft`` and ``fromRight``
+
 binary
 ~~~~~~
 
diff --git a/libraries/base/Data/Either.hs b/libraries/base/Data/Either.hs
index 8bef30b..437d87c 100644
--- a/libraries/base/Data/Either.hs
+++ b/libraries/base/Data/Either.hs
@@ -24,6 +24,8 @@ module Data.Either (
    rights,
    isLeft,
    isRight,
+   fromLeft,
+   fromRight,
    partitionEithers,
  ) where
 
@@ -280,6 +282,40 @@ isRight :: Either a b -> Bool
 isRight (Left  _) = False
 isRight (Right _) = True
 
+-- | Return the contents of a 'Left'-value or a default value otherwise.
+--
+-- @since 4.10.0.0
+--
+-- ==== __Examples__
+--
+-- Basic usage:
+--
+-- >>> fromLeft 1 (Left 3)
+-- 3
+-- >>> fromLeft 1 (Right "foo")
+-- 1
+--
+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.
+--
+-- @since 4.10.0.0
+--
+-- ==== __Examples__
+--
+-- Basic usage:
+--
+-- >>> fromRight 1 (Right 3)
+-- 3
+-- >>> fromRight 1 (Left "foo")
+-- 1
+--
+fromRight :: b -> Either a b -> b
+fromRight _ (Right b) = b
+fromRight b _         = b
+
 -- instance for the == Boolean type-level equality operator
 type family EqEither a b where
   EqEither ('Left x)  ('Left y)  = x == y
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index ecf6a82..996456f 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -14,6 +14,8 @@
   * `New modules `Data.Bifoldable` and `Data.Bitraversable` (previously defined
     in the `bifunctors` package) (#10448)
 
+  * `Data.Either` now provides `fromLeft` and `fromRight` (#12402)
+
 ## 4.9.0.0  *May 2016*
 
   * Bundled with GHC 8.0



More information about the ghc-commits mailing list