Copyright | (c) 2018 Kowainik |
---|---|
License | MIT |
Maintainer | Kowainik <xrom.xkov@gmail.com> |
Safe Haskell | Safe |
Language | Haskell2010 |
Relude.Extra.Enum
Description
Mini bounded-enum
framework inside relude
.
Documentation
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
for every value f a
. The
implementation makes sure that the Map
is constructed only once and then
shared for every call.
The complexity of reversed mapping though is O(logn).
Usually you want to use inverseMap
to inverse show
function.
>>>
data Color = Red | Green | Blue deriving (Show, Enum, Bounded)
>>>
parse = inverseMap show :: String -> Maybe Color
>>>
parse "Red"
Just Red>>>
parse "Black"
Nothing