module Data.WOE(
WOE(..)
) where
import Data.Tuple
import Data.Maybe
class Eq a => IsoEnum a where
mapping :: [(Int, a)]
data WOE a = WOE { unwrapWOE :: a }
toEnumSafely :: IsoEnum a => Int -> Maybe a
toEnumSafely n = lookup n mapping
fromEnumSafely :: IsoEnum a => a -> Maybe Int
fromEnumSafely x = lookup x $ swap <$> mapping
instance IsoEnum a => Enum (WOE a) where
toEnum = maybe (error "Invalid enum index.") WOE . toEnumSafely
fromEnum = maybe (error "Undefined enum index") id . fromEnumSafely . unwrapWOE