Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Test/QuickCheck/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Test.QuickCheck.Gen
, listOf
, vectorOf
, elements
, shuffle
, runGen
, evalGen
, perturbGen
Expand All @@ -39,7 +40,7 @@ import Control.Monad.State (State, runState, evalState)
import Control.Monad.State.Class (state, modify)
import Control.Monad.Gen.Class (class MonadGen)

import Data.Array ((!!), length)
import Data.Array ((!!), length, zip, sortBy)
import Data.Enum (class BoundedEnum, fromEnum, toEnum)
import Data.Foldable (fold)
import Data.Int (toNumber, floor)
Expand Down Expand Up @@ -212,6 +213,12 @@ elements (x :| xs) = do
n <- chooseInt zero (length xs)
pure if n == zero then x else fromMaybe x (xs !! (n - one))

-- | Generate a random permutation of the given array
shuffle :: forall a. Array a -> Gen (Array a)
shuffle xs = do
ns <- vectorOf (length xs) (chooseInt 0 top)
pure (map snd (sortBy (comparing fst) (zip ns xs)))

-- | Run a random generator
runGen :: forall a. Gen a -> GenState -> Tuple a GenState
runGen = runState <<< unGen
Expand Down