Skip to content

Commit d307db5

Browse files
authored
Add genBoundedEnum (#29)
* Add `genBoundedEnum` * Use `enumFromTo` for `genBoundedEnum` * Un-bias the enum generator
1 parent bf99e6a commit d307db5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Data/Enum/Gen.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Data.Enum.Gen where
2+
3+
import Prelude
4+
5+
import Control.Monad.Gen (class MonadGen, elements)
6+
import Data.Enum (class BoundedEnum, succ, enumFromTo)
7+
import Data.Maybe (Maybe(..))
8+
import Data.NonEmpty ((:|))
9+
10+
-- | Create a random generator for a finite enumeration.
11+
genBoundedEnum :: forall m a. MonadGen m => BoundedEnum a => m a
12+
genBoundedEnum =
13+
case succ bottom of
14+
Just a →
15+
let possibilities = enumFromTo a top :: Array a
16+
in elements (bottom :| possibilities)
17+
Nothing
18+
pure bottom

0 commit comments

Comments
 (0)