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 :: forall a. (Bounded a, Enum a) => NonEmpty a
- inverseMap :: forall a k. (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 :: forall a. (Bounded a, Enum a) => NonEmpty a Source #
Like universe
, but returns NonEmpty
list of some enumeration
>>>
universeNonEmpty :: NonEmpty Bool
False :| [True]
>>>
universeNonEmpty @Ordering
LT :| [EQ,GT]
>>>
data TrafficLight = Red | Blue | Green deriving (Show, Eq, Enum, Bounded)
>>>
universeNonEmpty :: NonEmpty TrafficLight
Red :| [Blue,Green]
>>>
data Singleton = Singleton deriving (Show, Eq, Enum, Bounded)
>>>
universeNonEmpty @Singleton
Singleton :| []
Since: 0.7.0.0
inverseMap :: forall a k. (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 Associativity | |
Defined in GHC.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.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.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.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.IO.IOMode | |
Enum Int16 | |
Enum Int32 | |
Enum Int64 | |
Enum Int8 | |
Enum DoCostCentres | |
Defined in GHC.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.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.RTS.Flags Methods enumFrom :: DoTrace -> [DoTrace] # enumFromThen :: DoTrace -> DoTrace -> [DoTrace] # enumFromTo :: DoTrace -> DoTrace -> [DoTrace] # enumFromThenTo :: DoTrace -> DoTrace -> DoTrace -> [DoTrace] # | |
Enum GiveGCStats | |
Defined in GHC.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.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.Word | |
Enum Word32 | |
Defined in GHC.Word | |
Enum Word64 | |
Defined in GHC.Word | |
Enum Word8 | |
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 Ordering | |
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 Integer | |
Enum Natural | |
Enum () | |
Enum Bool | |
Enum Char | |
Enum Int | |
Enum Levity | |
Defined in GHC.Enum | |
Enum VecCount | |
Defined in GHC.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.Enum Methods enumFrom :: VecElem -> [VecElem] # enumFromThen :: VecElem -> VecElem -> [VecElem] # enumFromTo :: VecElem -> VecElem -> [VecElem] # enumFromThenTo :: VecElem -> VecElem -> VecElem -> [VecElem] # | |
Enum Word | |
Enum a => Enum (And a) | |
Enum a => Enum (Iff a) | |
Enum a => Enum (Ior a) | |
Enum a => Enum (Xor a) | |
Enum a => Enum (Identity a) | |
Defined in 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 Data.Ord | |
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] # | |
Integral a => Enum (Ratio a) | |
Enum a => Enum (a) | |
Enum (Proxy s) | |
Enum a => Enum (Const a b) | |
Defined in 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 Data.Monoid | |
Enum (f a) => Enum (Alt f a) | |
a ~ b => Enum (a :~: b) | |
Defined in Data.Type.Equality Methods succ :: (a :~: b) -> a :~: b # pred :: (a :~: b) -> a :~: b # fromEnum :: (a :~: b) -> Int # enumFrom :: (a :~: b) -> [a :~: b] # enumFromThen :: (a :~: b) -> (a :~: b) -> [a :~: b] # enumFromTo :: (a :~: b) -> (a :~: b) -> [a :~: b] # enumFromThenTo :: (a :~: b) -> (a :~: b) -> (a :~: b) -> [a :~: b] # | |
a ~~ b => Enum (a :~~: b) | |
Defined in Data.Type.Equality Methods succ :: (a :~~: b) -> a :~~: b # pred :: (a :~~: b) -> a :~~: b # fromEnum :: (a :~~: b) -> Int # enumFrom :: (a :~~: b) -> [a :~~: b] # enumFromThen :: (a :~~: b) -> (a :~~: b) -> [a :~~: b] # enumFromTo :: (a :~~: b) -> (a :~~: b) -> [a :~~: b] # enumFromThenTo :: (a :~~: b) -> (a :~~: b) -> (a :~~: b) -> [a :~~: b] # |
Instances
boundedEnumFrom :: (Enum a, Bounded a) => a -> [a] #
boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a] #