Skip to content

Commit dc9040d

Browse files
committed
Merge pull request #36 from hdgarrood/better-arb-array
Better Arbitrary Array instance
2 parents 3e46bda + 392134e commit dc9040d

2 files changed

Lines changed: 4 additions & 8 deletions

File tree

src/Test/QuickCheck/Arbitrary.purs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Test.QuickCheck.Arbitrary where
22

33
import Prelude
44

5-
import Data.Array ((:))
65
import Data.Char (toCharCode, fromCharCode)
76
import Data.Either (Either(..))
87
import Data.Maybe (Maybe(..))
@@ -80,12 +79,7 @@ instance coarbOrdering :: Coarbitrary Ordering where
8079
coarbitrary GT = perturbGen 3.0
8180

8281
instance arbArray :: (Arbitrary a) => Arbitrary (Array a) where
83-
arbitrary = do
84-
b <- arbitrary
85-
if b then return [] else do
86-
a <- arbitrary
87-
as <- arbitrary
88-
return (a : as)
82+
arbitrary = arrayOf arbitrary
8983

9084
instance coarbArray :: (Coarbitrary a) => Coarbitrary (Array a) where
9185
coarbitrary = foldl (\f x -> f <<< coarbitrary x) id

src/Test/QuickCheck/Gen.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ arrayOf1 g = sized $ \n ->
127127

128128
-- | Create a random generator which generates a vector of random values of a specified size.
129129
vectorOf :: forall a. Int -> Gen a -> Gen (Array a)
130-
vectorOf k g = sequence $ const g <$> range one k
130+
vectorOf k g
131+
| k <= 0 = return []
132+
| otherwise = sequence $ const g <$> range one k
131133

132134
-- | Create a random generator which selects a value from a non-empty collection with
133135
-- | uniform probability.

0 commit comments

Comments
 (0)