| Copyright | (c) 2021-2023 Kowainik |
|---|---|
| License | MIT |
| Maintainer | Kowainik <xrom.xkov@gmail.com> |
| Stability | Stable |
| Portability | Portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Relude.Enum
Description
Reexports Enum related typeclasses and functions. Also introduces a few useful
helpers to work with Enums.
Note: universe, universeNonEmpty and inverseMap were previously in the
extra modules, but due to their benefit in different use cases. If you imported
Relude.Extra.Enum module, you can remove it now, as these functions are
reexported in the main Relude module.
Since: 1.0.0.0
Synopsis
- universe :: (Bounded a, Enum a) => [a]
- universeNonEmpty :: (Bounded a, Enum a) => NonEmpty a
- inverseMap :: (Bounded a, Enum a, Ord k) => (a -> k) -> k -> Maybe a
- class Enum a where
- succ :: a -> a
- pred :: a -> a
- toEnum :: Int -> a
- fromEnum :: a -> Int
- enumFrom :: a -> [a]
- enumFromThen :: a -> a -> [a]
- enumFromTo :: a -> a -> [a]
- enumFromThenTo :: a -> a -> a -> [a]
- class Bounded a where
- boundedEnumFrom :: (Enum a, Bounded a) => a -> [a]
- boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a]
Useful combinators for Enums
universe :: (Bounded a, Enum a) => [a] Source #
Returns all values of some Bounded Enum in ascending order.
>>>universe :: [Bool][False,True]
>>>universe @Ordering[LT,EQ,GT]
>>>data TrafficLight = Red | Blue | Green deriving (Show, Enum, Bounded)>>>universe :: [TrafficLight][Red,Blue,Green]
>>>data Singleton = Singleton deriving (Show, Enum, Bounded)>>>universe @Singleton[Singleton]
Since: 0.1.0
universeNonEmpty :: (Bounded a, Enum a) => NonEmpty a Source #
Like universe, but returns NonEmpty list of some enumeration
>>>universeNonEmpty :: NonEmpty BoolFalse :| [True]
>>>universeNonEmpty @OrderingLT :| [EQ,GT]
>>>data TrafficLight = Red | Blue | Green deriving (Show, Eq, Enum, Bounded)>>>universeNonEmpty :: NonEmpty TrafficLightRed :| [Blue,Green]
>>>data Singleton = Singleton deriving (Show, Eq, Enum, Bounded)>>>universeNonEmpty @SingletonSingleton :| []
Since: 0.7.0.0
inverseMap :: (Bounded a, Enum a, Ord k) => (a -> k) -> k -> Maybe a Source #
inverseMap f creates a function that is the inverse of a given function
f. It does so by constructing Map internally for each value f a. The
implementation makes sure that the Map is constructed only once and then
shared for every call.
Memory usage note: don't inverse functions that have types like Int
as their input. In this case the created Map will have huge size.
The complexity of reversed mapping is \(\mathcal{O}(\log n)\).
Performance note: make sure to specialize monomorphic type of your functions
that use inverseMap to avoid Map reconstruction.
One of the common inverseMap use-case is inverting the show or a show-like
function.
>>>data Color = Red | Green | Blue deriving (Show, Enum, Bounded)>>>parse = inverseMap show :: String -> Maybe Color>>>parse "Red"Just Red>>>parse "Black"Nothing
Correctness note: inverseMap expects injective function as its argument,
i.e. the function must map distinct arguments to distinct values.
Typical usage of this function looks like this:
data GhcVer
= Ghc802
| Ghc822
| Ghc844
| Ghc865
| Ghc881
deriving (Eq, Ord, Show, Enum, Bounded)
showGhcVer :: GhcVer -> Text
showGhcVer = \case
Ghc802 -> "8.0.2"
Ghc822 -> "8.2.2"
Ghc844 -> "8.4.4"
Ghc865 -> "8.6.5"
Ghc881 -> "8.8.1"
parseGhcVer :: Text -> Maybe GhcVer
parseGhcVer = inverseMap showGhcVer
Since: 0.1.1
Base reexports
Methods
enumFromThen :: a -> a -> [a] #
enumFromTo :: a -> a -> [a] #
enumFromThenTo :: a -> a -> a -> [a] #
Instances
| Enum Extension | |
Defined in GHC.LanguageExtensions.Type Methods succ :: Extension -> Extension # pred :: Extension -> Extension # fromEnum :: Extension -> Int # enumFrom :: Extension -> [Extension] # enumFromThen :: Extension -> Extension -> [Extension] # enumFromTo :: Extension -> Extension -> [Extension] # enumFromThenTo :: Extension -> Extension -> Extension -> [Extension] # | |
| Enum ByteOrder | |
Defined in GHC.Internal.ByteOrder Methods succ :: ByteOrder -> ByteOrder # pred :: ByteOrder -> ByteOrder # fromEnum :: ByteOrder -> Int # enumFrom :: ByteOrder -> [ByteOrder] # enumFromThen :: ByteOrder -> ByteOrder -> [ByteOrder] # enumFromTo :: ByteOrder -> ByteOrder -> [ByteOrder] # enumFromThenTo :: ByteOrder -> ByteOrder -> ByteOrder -> [ByteOrder] # | |
| Enum Associativity | |
Defined in GHC.Internal.Generics Methods succ :: Associativity -> Associativity # pred :: Associativity -> Associativity # toEnum :: Int -> Associativity # fromEnum :: Associativity -> Int # enumFrom :: Associativity -> [Associativity] # enumFromThen :: Associativity -> Associativity -> [Associativity] # enumFromTo :: Associativity -> Associativity -> [Associativity] # enumFromThenTo :: Associativity -> Associativity -> Associativity -> [Associativity] # | |
| Enum DecidedStrictness | |
Defined in GHC.Internal.Generics Methods succ :: DecidedStrictness -> DecidedStrictness # pred :: DecidedStrictness -> DecidedStrictness # toEnum :: Int -> DecidedStrictness # fromEnum :: DecidedStrictness -> Int # enumFrom :: DecidedStrictness -> [DecidedStrictness] # enumFromThen :: DecidedStrictness -> DecidedStrictness -> [DecidedStrictness] # enumFromTo :: DecidedStrictness -> DecidedStrictness -> [DecidedStrictness] # enumFromThenTo :: DecidedStrictness -> DecidedStrictness -> DecidedStrictness -> [DecidedStrictness] # | |
| Enum SourceStrictness | |
Defined in GHC.Internal.Generics Methods succ :: SourceStrictness -> SourceStrictness # pred :: SourceStrictness -> SourceStrictness # toEnum :: Int -> SourceStrictness # fromEnum :: SourceStrictness -> Int # enumFrom :: SourceStrictness -> [SourceStrictness] # enumFromThen :: SourceStrictness -> SourceStrictness -> [SourceStrictness] # enumFromTo :: SourceStrictness -> SourceStrictness -> [SourceStrictness] # enumFromThenTo :: SourceStrictness -> SourceStrictness -> SourceStrictness -> [SourceStrictness] # | |
| Enum SourceUnpackedness | |
Defined in GHC.Internal.Generics Methods succ :: SourceUnpackedness -> SourceUnpackedness # pred :: SourceUnpackedness -> SourceUnpackedness # toEnum :: Int -> SourceUnpackedness # fromEnum :: SourceUnpackedness -> Int # enumFrom :: SourceUnpackedness -> [SourceUnpackedness] # enumFromThen :: SourceUnpackedness -> SourceUnpackedness -> [SourceUnpackedness] # enumFromTo :: SourceUnpackedness -> SourceUnpackedness -> [SourceUnpackedness] # enumFromThenTo :: SourceUnpackedness -> SourceUnpackedness -> SourceUnpackedness -> [SourceUnpackedness] # | |
| Enum IOMode | |
Defined in GHC.Internal.IO.IOMode | |
| Enum Int16 | |
Defined in GHC.Internal.Int | |
| Enum Int32 | |
Defined in GHC.Internal.Int | |
| Enum Int64 | |
Defined in GHC.Internal.Int | |
| Enum Int8 | |
| Enum DoCostCentres | |
Defined in GHC.Internal.RTS.Flags Methods succ :: DoCostCentres -> DoCostCentres # pred :: DoCostCentres -> DoCostCentres # toEnum :: Int -> DoCostCentres # fromEnum :: DoCostCentres -> Int # enumFrom :: DoCostCentres -> [DoCostCentres] # enumFromThen :: DoCostCentres -> DoCostCentres -> [DoCostCentres] # enumFromTo :: DoCostCentres -> DoCostCentres -> [DoCostCentres] # enumFromThenTo :: DoCostCentres -> DoCostCentres -> DoCostCentres -> [DoCostCentres] # | |
| Enum DoHeapProfile | |
Defined in GHC.Internal.RTS.Flags Methods succ :: DoHeapProfile -> DoHeapProfile # pred :: DoHeapProfile -> DoHeapProfile # toEnum :: Int -> DoHeapProfile # fromEnum :: DoHeapProfile -> Int # enumFrom :: DoHeapProfile -> [DoHeapProfile] # enumFromThen :: DoHeapProfile -> DoHeapProfile -> [DoHeapProfile] # enumFromTo :: DoHeapProfile -> DoHeapProfile -> [DoHeapProfile] # enumFromThenTo :: DoHeapProfile -> DoHeapProfile -> DoHeapProfile -> [DoHeapProfile] # | |
| Enum DoTrace | |
Defined in GHC.Internal.RTS.Flags Methods enumFrom :: DoTrace -> [DoTrace] # enumFromThen :: DoTrace -> DoTrace -> [DoTrace] # enumFromTo :: DoTrace -> DoTrace -> [DoTrace] # enumFromThenTo :: DoTrace -> DoTrace -> DoTrace -> [DoTrace] # | |
| Enum GiveGCStats | |
Defined in GHC.Internal.RTS.Flags Methods succ :: GiveGCStats -> GiveGCStats # pred :: GiveGCStats -> GiveGCStats # toEnum :: Int -> GiveGCStats # fromEnum :: GiveGCStats -> Int # enumFrom :: GiveGCStats -> [GiveGCStats] # enumFromThen :: GiveGCStats -> GiveGCStats -> [GiveGCStats] # enumFromTo :: GiveGCStats -> GiveGCStats -> [GiveGCStats] # enumFromThenTo :: GiveGCStats -> GiveGCStats -> GiveGCStats -> [GiveGCStats] # | |
| Enum IoSubSystem | |
Defined in GHC.Internal.RTS.Flags Methods succ :: IoSubSystem -> IoSubSystem # pred :: IoSubSystem -> IoSubSystem # toEnum :: Int -> IoSubSystem # fromEnum :: IoSubSystem -> Int # enumFrom :: IoSubSystem -> [IoSubSystem] # enumFromThen :: IoSubSystem -> IoSubSystem -> [IoSubSystem] # enumFromTo :: IoSubSystem -> IoSubSystem -> [IoSubSystem] # enumFromThenTo :: IoSubSystem -> IoSubSystem -> IoSubSystem -> [IoSubSystem] # | |
| Enum Word16 | |
Defined in GHC.Internal.Word | |
| Enum Word32 | |
Defined in GHC.Internal.Word | |
| Enum Word64 | |
Defined in GHC.Internal.Word | |
| Enum Word8 | |
Defined in GHC.Internal.Word | |
| Enum Ordering | |
Defined in GHC.Internal.Enum | |
| Enum Undefined Source # | |
Defined in Relude.Debug Methods succ :: Undefined -> Undefined # pred :: Undefined -> Undefined # fromEnum :: Undefined -> Int # enumFrom :: Undefined -> [Undefined] # enumFromThen :: Undefined -> Undefined -> [Undefined] # enumFromTo :: Undefined -> Undefined -> [Undefined] # enumFromThenTo :: Undefined -> Undefined -> Undefined -> [Undefined] # | |
| Enum I8 | |
Defined in Data.Text.Foreign | |
| Enum FPFormat | |
Defined in Data.Text.Lazy.Builder.RealFloat Methods succ :: FPFormat -> FPFormat # pred :: FPFormat -> FPFormat # enumFrom :: FPFormat -> [FPFormat] # enumFromThen :: FPFormat -> FPFormat -> [FPFormat] # enumFromTo :: FPFormat -> FPFormat -> [FPFormat] # enumFromThenTo :: FPFormat -> FPFormat -> FPFormat -> [FPFormat] # | |
| Enum Integer | |
| Enum Natural | |
| Enum () | |
Defined in GHC.Internal.Enum | |
| Enum Bool | |
| Enum Char | |
| Enum Double | |
Defined in GHC.Internal.Float | |
| Enum Float | |
Defined in GHC.Internal.Float | |
| Enum Int | |
| Enum Levity | |
Defined in GHC.Internal.Enum | |
| Enum VecCount | |
Defined in GHC.Internal.Enum Methods succ :: VecCount -> VecCount # pred :: VecCount -> VecCount # enumFrom :: VecCount -> [VecCount] # enumFromThen :: VecCount -> VecCount -> [VecCount] # enumFromTo :: VecCount -> VecCount -> [VecCount] # enumFromThenTo :: VecCount -> VecCount -> VecCount -> [VecCount] # | |
| Enum VecElem | |
Defined in GHC.Internal.Enum Methods enumFrom :: VecElem -> [VecElem] # enumFromThen :: VecElem -> VecElem -> [VecElem] # enumFromTo :: VecElem -> VecElem -> [VecElem] # enumFromThenTo :: VecElem -> VecElem -> VecElem -> [VecElem] # | |
| Enum Word | |
| Enum a => Enum (First a) | |
Defined in Data.Semigroup Methods enumFrom :: First a -> [First a] # enumFromThen :: First a -> First a -> [First a] # enumFromTo :: First a -> First a -> [First a] # enumFromThenTo :: First a -> First a -> First a -> [First a] # | |
| Enum a => Enum (Last a) | |
Defined in Data.Semigroup | |
| Enum a => Enum (Max a) | |
Defined in Data.Semigroup | |
| Enum a => Enum (Min a) | |
Defined in Data.Semigroup | |
| Enum a => Enum (WrappedMonoid a) | |
Defined in Data.Semigroup Methods succ :: WrappedMonoid a -> WrappedMonoid a # pred :: WrappedMonoid a -> WrappedMonoid a # toEnum :: Int -> WrappedMonoid a # fromEnum :: WrappedMonoid a -> Int # enumFrom :: WrappedMonoid a -> [WrappedMonoid a] # enumFromThen :: WrappedMonoid a -> WrappedMonoid a -> [WrappedMonoid a] # enumFromTo :: WrappedMonoid a -> WrappedMonoid a -> [WrappedMonoid a] # enumFromThenTo :: WrappedMonoid a -> WrappedMonoid a -> WrappedMonoid a -> [WrappedMonoid a] # | |
| Enum a => Enum (Identity a) | |
Defined in GHC.Internal.Data.Functor.Identity Methods succ :: Identity a -> Identity a # pred :: Identity a -> Identity a # fromEnum :: Identity a -> Int # enumFrom :: Identity a -> [Identity a] # enumFromThen :: Identity a -> Identity a -> [Identity a] # enumFromTo :: Identity a -> Identity a -> [Identity a] # enumFromThenTo :: Identity a -> Identity a -> Identity a -> [Identity a] # | |
| (Enum a, Bounded a, Eq a) => Enum (Down a) | |
Defined in GHC.Internal.Data.Ord | |
| Integral a => Enum (Ratio a) | |
| Enum a => Enum (Solo a) | |
Defined in GHC.Internal.Enum | |
| Enum (Fixed a) | |
Defined in Data.Fixed Methods enumFrom :: Fixed a -> [Fixed a] # enumFromThen :: Fixed a -> Fixed a -> [Fixed a] # enumFromTo :: Fixed a -> Fixed a -> [Fixed a] # enumFromThenTo :: Fixed a -> Fixed a -> Fixed a -> [Fixed a] # | |
| Enum (Proxy s) | |
| Enum a => Enum (Const a b) | |
Defined in GHC.Internal.Data.Functor.Const Methods succ :: Const a b -> Const a b # pred :: Const a b -> Const a b # fromEnum :: Const a b -> Int # enumFrom :: Const a b -> [Const a b] # enumFromThen :: Const a b -> Const a b -> [Const a b] # enumFromTo :: Const a b -> Const a b -> [Const a b] # enumFromThenTo :: Const a b -> Const a b -> Const a b -> [Const a b] # | |
| Enum (f a) => Enum (Ap f a) | |
Defined in GHC.Internal.Data.Monoid | |
| Enum (f a) => Enum (Alt f a) | |
| Enum (f (g a)) => Enum (Compose f g a) | |
Defined in Data.Functor.Compose Methods succ :: Compose f g a -> Compose f g a # pred :: Compose f g a -> Compose f g a # toEnum :: Int -> Compose f g a # fromEnum :: Compose f g a -> Int # enumFrom :: Compose f g a -> [Compose f g a] # enumFromThen :: Compose f g a -> Compose f g a -> [Compose f g a] # enumFromTo :: Compose f g a -> Compose f g a -> [Compose f g a] # enumFromThenTo :: Compose f g a -> Compose f g a -> Compose f g a -> [Compose f g a] # | |
Instances
boundedEnumFrom :: (Enum a, Bounded a) => a -> [a] #
boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a] #