Expose internal Int in IDs and make the type an instance of Random (generating only positive IDs)

This commit is contained in:
Tissevert 2019-11-12 22:15:39 +01:00
parent e0003c5906
commit 3440c84543
1 changed files with 12 additions and 5 deletions

View File

@ -1,10 +1,17 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Hanafuda.ID (
ID(..)
, getID
ID(..)
) where
newtype ID a = ID Int deriving (Eq, Ord, Enum, Read, Show)
import System.Random (Random(..))
getID :: ID a -> String
getID (ID n) = show n
newtype ID a = ID {
getID :: Int
} deriving (Eq, Ord, Enum, Read, Show)
rIntToRID :: (Int, g) -> (ID a, g)
rIntToRID (someID, g) = (ID someID, g)
instance Random (ID a) where
random = rIntToRID . randomR (0, maxBound)
randomR (ID idA, ID idB) = rIntToRID . randomR (idA, idB)