@@ -3,12 +3,15 @@ module Control.Monad.Gen.Common where
3
3
import Prelude
4
4
5
5
import Control.Apply (lift2 )
6
- import Control.Monad.Gen (class MonadGen , choose , chooseFloat )
6
+ import Control.Monad.Gen (class MonadGen , resize , choose , chooseFloat , unfoldable )
7
+ import Control.Monad.Rec.Class (class MonadRec )
7
8
8
9
import Data.Either (Either (..))
9
10
import Data.Identity (Identity (..))
10
11
import Data.Maybe (Maybe (..))
12
+ import Data.NonEmpty (NonEmpty , (:|))
11
13
import Data.Tuple (Tuple (..))
14
+ import Data.Unfoldable (class Unfoldable )
12
15
13
16
-- | Creates a generator that outputs `Either` values, choosing a value from a
14
17
-- | `Left` or the `Right` with even probability.
@@ -34,3 +37,18 @@ genMaybe gen = do
34
37
-- | pair of generators for each slot in the tuple.
35
38
genTuple :: forall m a b . Apply m => m a -> m b -> m (Tuple a b )
36
39
genTuple = lift2 Tuple
40
+
41
+ -- | Creates a generator that outputs `NonEmpty` values, choosing values from a
42
+ -- | generator for each of the items.
43
+ -- |
44
+ -- | The size of the value will be determined by the current size state
45
+ -- | for the generator. To generate a value of a particular size, use the
46
+ -- | `resize` function from the `MonadGen` class first.
47
+ genNonEmpty
48
+ :: forall m a f
49
+ . MonadRec m
50
+ => MonadGen m
51
+ => Unfoldable f
52
+ => m a
53
+ -> m (NonEmpty f a )
54
+ genNonEmpty gen = (:|) <$> gen <*> resize (_ - 1 ) (unfoldable gen)
0 commit comments