{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TypeApplications #-}{- |Copyright: (c) 2018 KowainikLicense: MITMaintainer: Kowainik <xrom.xkov@gmail.com>Mini @bounded-enum@ framework inside @relude@.-}module Relude.Extra.Enum ( universe , inverseMap , next , prec , safeToEnum ) whereimport Reludeimport qualified Data.Map.Strict as M-- $setup-- >>> :set -XTypeApplications{- | Returns all values of some 'Bounded' 'Enum' in ascending order.>>> data TrafficLight = Red | Blue | Green deriving (Show, Enum, Bounded)>>> universe :: [TrafficLight][Red,Blue,Green]>>> universe :: [Bool][False,True]-}universe :: (Bounded a, Enum a) => [a]universe = [minBound .. maxBound]{- | @inverseMap f@ creates a function that is the inverse of a given function@f@. It does so by constructing 'M.Map' for every value @f a@. Theimplementation makes sure that the 'M.Map' is constructed only once and thenshared for every call.The complexity of reversed mapping though is \(\mathcal{O}(\log n)\).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-}inverseMap :: forall a k . (Bounded a, Enum a, Ord k) => (a -> k) -> (k -> Maybe a)inverseMap f = \k -> M.lookup k dict where dict :: M.Map k a dict = M.fromList $ zip (map f univ) univ univ :: [a] univ = universe{- | Like 'succ', but doesn't fail on 'maxBound'. Instead it returns 'minBound'.>>> next FalseTrue>>> next TrueFalse>>> succ True*** Exception: Prelude.Enum.Bool.succ: bad argument-}next :: (Eq a, Bounded a, Enum a) => a -> anext e | e == maxBound = minBound | otherwise = succ e{- | Like 'pred', but doesn't fail on 'minBound'. Instead it returns 'maxBound'.>>> prec FalseTrue>>> prec TrueFalse>>> pred False*** Exception: Prelude.Enum.Bool.pred: bad argument-}prec :: (Eq a, Bounded a, Enum a) => a -> aprec e | e == minBound = maxBound | otherwise = pred e{- | Returns 'Nothing' if given 'Int' outside range.>>> safeToEnum @Bool 0Just False>>> safeToEnum @Bool 1Just True>>> safeToEnum @Bool 2Nothing>>> safeToEnum @Bool (-1)Nothing-}safeToEnum :: forall a . (Bounded a, Enum a) => Int -> Maybe asafeToEnum i = guard (fromEnum @a minBound <= i && i <= fromEnum @a maxBound) *> Just (toEnum i)