Skip to content

Commit

Permalink
Add genBoundedEnum (#29)
Browse files Browse the repository at this point in the history
* Add `genBoundedEnum`

* Use `enumFromTo` for `genBoundedEnum`

* Un-bias the enum generator
  • Loading branch information
garyb authored Jun 8, 2017
1 parent bf99e6a commit d307db5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Data/Enum/Gen.purs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d307db5

Please sign in to comment.