File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -103,10 +103,17 @@ unfoldable gen = unfoldr unfold <$> sized (tailRecM loopGen <<< Tuple Nil)
103
103
-- | matches a given predicate. This will never halt if the predicate always
104
104
-- | fails.
105
105
suchThat :: forall m a . MonadRec m => MonadGen m => m a -> (a -> Boolean ) -> m a
106
- suchThat gen pred = tailRecM go unit
106
+ suchThat gen pred = filtered $ gen <#> \a -> if pred a then Just a else Nothing
107
+
108
+ -- | Creates a generator that repeatedly run another generator until it produces
109
+ -- | `Just` node. This will never halt if the input generatr always produces Nothing.
110
+ filtered :: forall m a b . MonadRec m => MonadGen m => m (Maybe a ) -> m a
111
+ filtered gen = tailRecM go unit
107
112
where
108
113
go :: Unit -> m (Step Unit a )
109
- go _ = gen <#> \a -> if pred a then Done a else Loop unit
114
+ go _ = gen <#> \a -> case a of
115
+ Nothing -> Loop unit
116
+ Just a -> Done a
110
117
111
118
fromIndex :: forall f a . Foldable f => Int -> a -> f a -> a
112
119
fromIndex i a = fromMaybe a <<< snd <<< (foldl go (Tuple 0 (Just a)))
You can’t perform that action at this time.
0 commit comments