diff --git a/src/Data/Enum/Gen.purs b/src/Data/Enum/Gen.purs new file mode 100644 index 0000000..86caebd --- /dev/null +++ b/src/Data/Enum/Gen.purs @@ -0,0 +1,18 @@ +module Data.Enum.Gen where + +import Prelude + +import Control.Monad.Gen (class MonadGen, elements) +import Data.Enum (class BoundedEnum, succ, enumFromTo) +import Data.Maybe (Maybe(..)) +import Data.NonEmpty ((:|)) + +-- | Create a random generator for a finite enumeration. +genBoundedEnum :: forall m a. MonadGen m => BoundedEnum a => m a +genBoundedEnum = + case succ bottom of + Just a → + let possibilities = enumFromTo a top :: Array a + in elements (bottom :| possibilities) + Nothing → + pure bottom