{-# LANGUAGE Safe #-}
module Relude.Extra.Enum
( next
, prev
, safeToEnum
) where
import Relude
next :: (Eq a, Bounded a, Enum a) => a -> a
next :: forall a. (Eq a, Bounded a, Enum a) => a -> a
next a
e
| a
e forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
maxBound = forall a. Bounded a => a
minBound
| Bool
otherwise = forall a. Enum a => a -> a
succ a
e
{-# INLINE next #-}
prev :: (Eq a, Bounded a, Enum a) => a -> a
prev :: forall a. (Eq a, Bounded a, Enum a) => a -> a
prev a
e
| a
e forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = forall a. Bounded a => a
maxBound
| Bool
otherwise = forall a. Enum a => a -> a
pred a
e
{-# INLINE prev #-}
safeToEnum :: forall a . (Bounded a, Enum a) => Int -> Maybe a
safeToEnum :: forall a. (Bounded a, Enum a) => Int -> Maybe a
safeToEnum Int
i = forall (f :: * -> *). Alternative f => Bool -> f ()
guard (forall a. Enum a => a -> Int
fromEnum @a forall a. Bounded a => a
minBound forall a. Ord a => a -> a -> Bool
<= Int
i Bool -> Bool -> Bool
&& Int
i forall a. Ord a => a -> a -> Bool
<= forall a. Enum a => a -> Int
fromEnum @a forall a. Bounded a => a
maxBound) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> forall a. Enum a => Int -> a
toEnum Int
i
{-# INLINE safeToEnum #-}