[Git][ghc/ghc][master] base: Introduce Data.Enum.enumerate (CLC #306)
Marge Bot (@marge-bot)
gitlab at gitlab.haskell.org
Wed Jan 29 07:28:58 UTC 2025
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
a963a1a5 by Marc Scholten at 2025-01-29T02:28:35-05:00
base: Introduce Data.Enum.enumerate (CLC #306)
https://github.com/haskell/core-libraries-committee/issues/306
- - - - -
6 changed files:
- libraries/base/changelog.md
- libraries/base/src/Data/Enum.hs
- testsuite/tests/interface-stability/base-exports.stdout
- testsuite/tests/interface-stability/base-exports.stdout-javascript-unknown-ghcjs
- testsuite/tests/interface-stability/base-exports.stdout-mingw32
- testsuite/tests/interface-stability/base-exports.stdout-ws-32
Changes:
=====================================
libraries/base/changelog.md
=====================================
@@ -9,6 +9,7 @@
* `Data.List.NonEmpty.{init,last,tails1}` are now defined using only total functions (rather than partial ones). ([CLC proposal #293](https://github.com/haskell/core-libraries-committee/issues/293))
* `Data.List.NonEmpty` functions now have the same laziness as their `Data.List` counterparts (i.e. make them more strict than they currently are) ([CLC proposal #107](https://github.com/haskell/core-libraries-committee/issues/107))
* `instance Functor NonEmpty` is now specified using `map` (rather than duplicating code). ([CLC proposal #300](https://github.com/haskell/core-libraries-committee/issues/300))
+ * The `Data.Enum.enumerate` function was introduced ([CLC #306](https://github.com/haskell/core-libraries-committee/issues/306))
## 4.21.0.0 *TBA*
* Change `SrcLoc` to be a strict and unboxed (finishing [CLC proposal #55](https://github.com/haskell/core-libraries-committee/issues/55))
=====================================
libraries/base/src/Data/Enum.hs
=====================================
@@ -21,6 +21,34 @@ module Data.Enum
( Enum(..)
, {-# DEPRECATED "Bounded should be imported from Data.Bounded" #-}
Bounded(..)
+ , enumerate
) where
import GHC.Internal.Enum
+
+-- | Returns a list of all values of an enum type
+--
+-- 'enumerate' is often used to list all values of a custom enum data structure, such as a custom Color enum below:
+--
+-- @
+-- data Color = Yellow | Red | Blue
+-- deriving (Enum, Bounded, Show)
+--
+-- allColors :: [Color]
+-- allColors = enumerate
+-- -- Result: [Yellow, Red, Blue]
+-- @
+--
+-- Note that you need to derive the 'Bounded' type class as well, only 'Enum' is not enough.
+-- 'Enum' allows for sequential enumeration, while 'Bounded' provides the 'minBound' and 'maxBound' values.
+--
+-- 'enumerate' is commonly used together with the TypeApplications syntax. Here is an example of using 'enumerate' to retrieve all values of the 'Ordering' type:
+--
+-- >> enumerate @Ordering
+-- [LT, EQ, GT]
+--
+-- The '@' symbol here is provided by the TypeApplications language extension.
+--
+-- @since base-4.22.0.0
+enumerate :: (Enum a, Bounded a) => [a]
+enumerate = [minBound .. maxBound]
\ No newline at end of file
=====================================
testsuite/tests/interface-stability/base-exports.stdout
=====================================
@@ -979,6 +979,7 @@ module Data.Enum where
enumFromTo :: a -> a -> [a]
enumFromThenTo :: a -> a -> a -> [a]
{-# MINIMAL toEnum, fromEnum #-}
+ enumerate :: forall a. (Enum a, Bounded a) => [a]
module Data.Eq where
-- Safety: Safe
=====================================
testsuite/tests/interface-stability/base-exports.stdout-javascript-unknown-ghcjs
=====================================
@@ -979,6 +979,7 @@ module Data.Enum where
enumFromTo :: a -> a -> [a]
enumFromThenTo :: a -> a -> a -> [a]
{-# MINIMAL toEnum, fromEnum #-}
+ enumerate :: forall a. (Enum a, Bounded a) => [a]
module Data.Eq where
-- Safety: Safe
=====================================
testsuite/tests/interface-stability/base-exports.stdout-mingw32
=====================================
@@ -979,6 +979,7 @@ module Data.Enum where
enumFromTo :: a -> a -> [a]
enumFromThenTo :: a -> a -> a -> [a]
{-# MINIMAL toEnum, fromEnum #-}
+ enumerate :: forall a. (Enum a, Bounded a) => [a]
module Data.Eq where
-- Safety: Safe
=====================================
testsuite/tests/interface-stability/base-exports.stdout-ws-32
=====================================
@@ -979,6 +979,7 @@ module Data.Enum where
enumFromTo :: a -> a -> [a]
enumFromThenTo :: a -> a -> a -> [a]
{-# MINIMAL toEnum, fromEnum #-}
+ enumerate :: forall a. (Enum a, Bounded a) => [a]
module Data.Eq where
-- Safety: Safe
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/a963a1a5d38cd6fbb722f3d25c31744238d71b65
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/a963a1a5d38cd6fbb722f3d25c31744238d71b65
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20250129/9ba832c6/attachment-0001.html>
More information about the ghc-commits
mailing list