Skip to content

Add genBoundedEnum #29

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

Merged
merged 3 commits into from
Jun 8, 2017
Merged

Add genBoundedEnum #29

merged 3 commits into from
Jun 8, 2017

Conversation

garyb
Copy link
Member

@garyb garyb commented Jun 8, 2017

No description provided.

-- | `fromEnum bottom` and `fromEnum top`.
genBoundedEnum :: forall m a. MonadGen m => BoundedEnum a => m a
genBoundedEnum =
unsafePartial fromJust <<< toEnum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unsafePartial is only safe if the Enum instance is valid, I think.

Another option is oneOf (bottom .. top), or something along those lines, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generator I lifted from QuickCheck actually, it was recently added there, so it suffers from any of the same problems that that one has.

Can you explain what you're thinking with oneOf (bottom .. top), I'm not sure I follow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does say in the comment that toEnum must be well behaved too 😉

Copy link
Contributor

@paf31 paf31 Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By bottom .. top, I meant to say enumFromTo bottom top, which must be safe, I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yeah, that'd work. It'll be kinda expensive if used for large enums, but can revisit if we have a case where it's obviously terrible. :)

@garyb
Copy link
Member Author

garyb commented Jun 8, 2017

How's this? A slight bias to bottom now, but necessary without adding dependencies or FFI something, since there's no array/list dependency even transitively.

@paf31
Copy link
Contributor

paf31 commented Jun 8, 2017

👍 Thanks!

@garyb garyb merged commit d307db5 into master Jun 8, 2017
@garyb garyb deleted the gen branch June 8, 2017 20:27
hdgarrood pushed a commit that referenced this pull request Jul 29, 2017
* Add `genBoundedEnum`

* Use `enumFromTo` for `genBoundedEnum`

* Un-bias the enum generator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants