Skip to content
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

Add randomSampleOne #114

Merged
merged 6 commits into from
Nov 3, 2020
Merged

Add randomSampleOne #114

merged 6 commits into from
Nov 3, 2020

Conversation

JordanMartinez
Copy link
Contributor

Fixes #113

generate :: forall a. Gen a -> Effect a
generate gen = do
seed <- randomSeed
pure $ evalGen gen { newSeed: seed, size: 1 }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think 1 is probably too small for the size parameter, as that will probably restrict the values which can come out; we should pick a size large enough to ensure that most/all of the values this generator can produce can be returned here. The existing randomSample uses a size of 10, for instance. I'm not sure whether 10 is a good default here though. Can you give this a try with some common generators and see what you get?

I also think it's not ideal that the name generate doesn't indicate that this function has anything to do with the existing randomSample function, even though they're very similar. I think I'd prefer singleSample or sampleOne or something. Does anyone else have ideas?

Copy link
Member

Choose a reason for hiding this comment

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

A size of 1 is definitely too small, that'd result in a 1-element array, or one level of structure in a sized recursive generator.

I think 10 seems reasonable, and it won't override a Gen that is resized to more or less than that too. A quick check of some common generators with that size would be good just to double check, but I'd probably have chosen 10 if I were writing it too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also think it's not ideal that the name generate doesn't indicate that this function has anything to do with the existing randomSample function, even though they're very similar. I think I'd prefer singleSample or sampleOne or something. Does anyone else have ideas?

What about randomOne? If one starts typing ran for either one of these functions, it'll show all three of them in the dropdown.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really like randomOne because I think the "sample" is more important than the "random" in randomSample; what you're doing is sampling from the generator. Perhaps randomSampleOne? That way it is clearly a variant of randomSample, and they'll still all turn up in the completion dropdowns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

-- | Sample a random generator, using a randomly generated seed
randomSample' :: forall a. Size -> Gen a -> Effect (Array a)
randomSample' n g = do
seed <- randomSeed
pure $ sample seed n g

-- | Get a random sample of 10 values
-- | Get a random sample of 10 values. For a single value, use `randomOne`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment needs updating now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@JordanMartinez JordanMartinez merged commit da51c3d into purescript:master Nov 3, 2020
@JordanMartinez JordanMartinez deleted the addGenerate branch November 3, 2020 15:23
@JordanMartinez JordanMartinez changed the title Add generate Add randomSampleOne Nov 3, 2020
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.

Provide Gen a -> Effect a function for ease of use.
3 participants