summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Meysmans <contact@mettekou.com>2016-07-20 09:54:55 +0200
committerBen Gamari <ben@smart-cactus.org>2016-07-20 15:17:50 +0200
commita0f83a628cc6a00f948662f88e711c2a37bfda60 (patch)
tree0e69761cd0e5735a1704dd2f8c08cca1ab81fec5
parentcac3fb06f4b282eee21159c364c4d08e8fdedce9 (diff)
downloadhaskell-a0f83a628cc6a00f948662f88e711c2a37bfda60.tar.gz
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
-rw-r--r--docs/users_guide/8.2.1-notes.rst2
-rw-r--r--libraries/base/Data/Either.hs36
-rw-r--r--libraries/base/changelog.md2
3 files changed, 40 insertions, 0 deletions
diff --git a/docs/users_guide/8.2.1-notes.rst b/docs/users_guide/8.2.1-notes.rst
index 5f45bf1002..27b49efa7e 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 8bef30be9c..437d87cb5e 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 ecf6a82df6..996456f0e6 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