Skip to content

Commit f278bdb

Browse files
authored
Merge pull request #69 from matthewleon/shuffle
add shuffle generator
2 parents 6603ff4 + 6eaba77 commit f278bdb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Test/QuickCheck/Gen.purs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Test.QuickCheck.Gen
2020
, listOf
2121
, vectorOf
2222
, elements
23+
, shuffle
2324
, runGen
2425
, evalGen
2526
, perturbGen
@@ -39,7 +40,7 @@ import Control.Monad.State (State, runState, evalState)
3940
import Control.Monad.State.Class (state, modify)
4041
import Control.Monad.Gen.Class (class MonadGen)
4142

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

216+
-- | Generate a random permutation of the given array
217+
shuffle :: forall a. Array a -> Gen (Array a)
218+
shuffle xs = do
219+
ns <- vectorOf (length xs) (chooseInt 0 top)
220+
pure (map snd (sortBy (comparing fst) (zip ns xs)))
221+
215222
-- | Run a random generator
216223
runGen :: forall a. Gen a -> GenState -> Tuple a GenState
217224
runGen = runState <<< unGen

0 commit comments

Comments
 (0)