-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop using fromJust #339
Stop using fromJust #339
Conversation
Here's my best guess about what this stuff is supposed to do. Please review carefully. |
094ffc7
to
fe4b046
Compare
@@ -256,8 +259,7 @@ runGenT size seed (GenT m) = | |||
-- | |||
evalGen :: Size -> Seed -> Gen a -> Maybe (Tree a) | |||
evalGen size seed = | |||
fmap (fmap Maybe.fromJust) . | |||
Tree.filter Maybe.isJust . | |||
Tree.mapMaybe id . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a substantial change. Please be especially careful to check that it makes sense!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provided Tree.mapMaybe
works then this looks like it would be fine.
* Get rid of all but one use of `fromJust`. * Expose `mapMaybe`-like functions along with `filter`-like ones. Fixes hedgehogqa#301
fe4b046
to
c6b0bff
Compare
hedgehog/hedgehog.cabal
Outdated
other-modules: | ||
-- This has nothing to do with Hedgehog proper, and it's | ||
-- too trivial to bother exposing. | ||
Hedgehog.Internal.Predicate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the comment, but would like to maintain a policy of always exposing all internal modules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#339 (comment), most likely we don't even need to introduce a new module.
I added a stupid unit test just to mess around with it in the repl, looks pretty good to me |
Just have a few style guide things, will make some suggestions |
@@ -1270,34 +1271,38 @@ ensure p gen = do | |||
-- forever. If we trigger these limits then the whole generator is discarded. | |||
-- | |||
filter :: (MonadGen m, GenBase m ~ Identity) => (a -> Bool) -> m a -> m a | |||
filter p gen0 = | |||
filter p = mapMaybe (fromPred p) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filter p = mapMaybe (fromPred p) | |
filter p = | |
mapMaybe (fromPred p) |
else | ||
try (k + 1) | ||
case p x of | ||
Just _ -> withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just _ -> withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen | |
Just _ -> | |
withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen |
try (k + 1) | ||
case p x of | ||
Just _ -> withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen | ||
Nothing -> try (k + 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing -> try (k + 1) | |
Nothing -> | |
try (k + 1) |
in | ||
try 0 | ||
|
||
filterT :: MonadGen m => (a -> Bool) -> m a -> m a | ||
filterT p gen0 = | ||
filterT p = mapMaybeT (fromPred p) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filterT p = mapMaybeT (fromPred p) | |
filterT p = | |
mapMaybeT (fromPred p) |
else | ||
try (k + 1) | ||
case p x of | ||
Just _ -> withGenT (mapGenT (Tree.mapMaybeT p)) gen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just _ -> withGenT (mapGenT (Tree.mapMaybeT p)) gen | |
Just _ -> | |
withGenT (mapGenT (Tree.mapMaybeT p)) gen |
try (k + 1) | ||
case p x of | ||
Just _ -> withGenT (mapGenT (Tree.mapMaybeT p)) gen | ||
Nothing -> try (k + 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing -> try (k + 1) | |
Nothing -> | |
try (k + 1) |
-- | Convert a Boolean predicate to something suitable for use | ||
-- with @mapMaybe@-like functions. The @wizards@ package calls this | ||
-- @ensure@. @protolude@ and @relude@ generalize from 'Maybe' to | ||
-- arbitrary 'Control.Applicative.Alternative' and call it @guarded@. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Extreme nitpick) - need to push those comments on line (8-10) two space deeper, then add an empty --
line, if we want this to be consistent with all the other commented stuff, like so:
-- | Convert a Boolean predicate to something suitable for use
-- with @mapMaybe@-like functions. The @wizards@ package calls this
-- @ensure@. @protolude@ and @relude@ generalize from 'Maybe' to
-- arbitrary 'Control.Applicative.Alternative' and call it @guarded@.
--
(Absolutely not a blocking issue, of course!) 📨
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we even want a dedicated module for this, as we can just declare this in both Gen
and Tree
(but not expose it). Then, if we need to use it on a third module, we can consider the rule of three and move it to its own module.
It's just that Hedgehog.Internal.Predicate looks like a utility module that we don't really need (yet)...
module Hedgehog.Internal.Predicate | ||
( fromPred | ||
) where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't do multiline suggestions but
- module Hedgehog.Internal.Predicate
- ( fromPred
+ module Hedgehog.Internal.Predicate (
+ fromPred
) where
Nothing -> mxs | ||
Just x -> | ||
case p x of | ||
Just x' -> [Tree (Node x' mxs)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just x' -> [Tree (Node x' mxs)] | |
Just x' -> | |
[Tree (Node x' mxs)] |
Just x -> | ||
case p x of | ||
Just x' -> [Tree (Node x' mxs)] | ||
Nothing -> mxs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing -> mxs | |
Nothing -> | |
mxs |
|
||
-- | Returns a tree containing only elements that match the predicate. | ||
-- | ||
-- When an element does not match the predicate its node is replaced with | ||
-- 'empty'. | ||
-- | ||
filterT :: (Monad m, Alternative m) => (a -> Bool) -> TreeT m a -> TreeT m a | ||
filterT p m = | ||
filterT p = mapMaybeT (fromPred p) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filterT p = mapMaybeT (fromPred p) | |
filterT p = | |
mapMaybeT (fromPred p) |
Sorry for all the style nits, appreciate the contribution! |
1b8b97f
to
392b846
Compare
@treeowl would you like me or @jacobstanley to apply those few style nits above? Happy to do so if you're busy. After that we should be good to go and merge this 🚀 |
Go ahead. I'm sluggish today.
…On Mon, Nov 18, 2019, 10:26 AM Nikos Baxevanis ***@***.***> wrote:
@treeowl <https://github.com/treeowl> would you like me or @jacobstanley
<https://github.com/jacobstanley> to apply those few style nits above?
Happy to do so if you're busy. After that we should be good to go and merge
this 🚀
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#339?email_source=notifications&email_token=AAOOF7KVY2YW653LWVL3DPTQUKX4BA5CNFSM4JMJEQQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEKZ6EA#issuecomment-555065104>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAOOF7MWHYFLNVUJEJUO5SLQUKX4BANCNFSM4JMJEQQA>
.
|
Get rid of all but one use of
fromJust
.Fixes #301