Rename "range" to "uniform"#395
Conversation
|
I reexported |
|
To be honest I feel like this PR is a bit premature, but I'm not really sure what to say. @dhardy, can I leave things to you 😄? I believe if we want to have
We don't need compatibility reexports for the |
Why? They are basically the same. I could imagine to have type aliases.
👍 |
|
I like the name Either way there are a few other changes in this PR we want (finishing the rename from Uniform → Standard). |
dhardy
left a comment
There was a problem hiding this comment.
After some thinking about this change, it makes a lot of sense. We are emphasising how variables are sampled, not the domain in which they are sampled.
This also opens up some possible additional constructors like Uniform::default() and Uniform::full_range() — although the former is redundant with Standard and the latter doesn't make sense for floating-point types.
I think Standard still has an important role to play: it is simpler, thus easier to understand and probably faster to compile code using it. Also Standard uses no memory.
src/distributions/mod.rs
Outdated
| pub use self::other::Alphanumeric; | ||
| pub use self::range::Range; | ||
| pub use self::uniform::Uniform; | ||
| #[deprecated(since="0.5.0", note="use Distribution instead")] |
There was a problem hiding this comment.
Shouldn't this say "use Uniform instead"?
| pub use distributions::uniform::Uniform as Range; | ||
| pub use distributions::uniform::UniformInt as RangeInt; | ||
| pub use distributions::uniform::UniformFloat as RangeFloat; | ||
| pub use distributions::uniform::SampleUniform as SampleRange; |
There was a problem hiding this comment.
I don't think there's any point exporting the last three here because RangeInt and RangeFloat were never in a release and SampleRange is totally different.
There was a problem hiding this comment.
I agree about Range* and removed them. (I was not aware they were not released.)
However, what do you mean about SampleRange? It was public (although not supposed to be used), so removing is a breaking change, hence the deprecated reexport.
There was a problem hiding this comment.
Oh, so you are saying it is a breaking change anyway? I still think it makes sense to have it deprecated for consistency. As you said, it might possibly keep some code working this way, but I suspect it was not used much.
IMO it's much better to get this right now than after the 0.5 release.
No, having two names for the same thing makes no sense IMO (except as deprecated re-export as in this PR). |
pitdicker
left a comment
There was a problem hiding this comment.
Either way there are a few other changes in this PR we want (finishing the rename from Uniform → Standard).
👍
After some thinking about this change, it makes a lot of sense. We are emphasising how variables are sampled, not the domain in which they are sampled.
Purely thinking within the domain it makes sense. As long as we keep the name Rng::gen_range, and don't purge the name "range" entirely from the documentation, I think it is ok.
I left some small notes on the documentation.
| fn ind_sample<R: Rng>(&self, &mut R) -> Support; | ||
| } | ||
|
|
||
| /// DEPRECATED: Use `distributions::uniform` instead. |
There was a problem hiding this comment.
Nit (sorry): distributions::Uniform
There was a problem hiding this comment.
The spelling is as intended, because it addresses the module and not the type.
src/distributions/uniform.rs
Outdated
| /// Sample values uniformly between two bounds. | ||
| /// | ||
| /// `Range::new` and `Range::new_inclusive` will set up a `Range`, which does | ||
| /// `Uniform::new` and `Uniform::new_inclusive` will set up a `Uniform`, which does |
There was a problem hiding this comment.
I don't think this sentence read well any more, but have difficulty saying why.
Can you also mention "range" once in this description?
There was a problem hiding this comment.
You're right. Perhaps something like U::new and U::new_inclusive construct a Uniform distribution sampling from the half-open and closed (inclusive) ranges respectively.
| /// repeatedly with the same arguments, one should use `Uniform`, as | ||
| /// that will amortize the computations that allow for perfect | ||
| /// uniformity, as they only happen when constructing the `Range`. | ||
| /// uniformity, as they only happen when constructing the `Uniform`. |
There was a problem hiding this comment.
Can we mention "range" one or two times in this comment? I don't want to see no mention at all to this common term any more.
There was a problem hiding this comment.
It is already mentioned in the name and the fist sentence.
There was a problem hiding this comment.
I agree with @pitdicker actually that a couple of things could be changed:
- "wrapper around
distributions::Uniform::sample_single" - "when constructing the
Uniformrange."
|
I will rebase after review is finished. |
dhardy
left a comment
There was a problem hiding this comment.
I see you pushed the From<ops::Range<X>> support into the same PR; I think we're all happy with that?
src/distributions/uniform.rs
Outdated
| /// Implementation of `UniformImpl` for integer types. | ||
| /// | ||
| /// Unless you are implementing `RangeImpl` for your own type, this type should | ||
| /// not be used directly, use `Range` instead. |
There was a problem hiding this comment.
You forgot to update the Range names
There was a problem hiding this comment.
Sorry, this is a left-over from the merge...
| fn from(r: ::core::ops::Range<X>) -> Uniform<X> { | ||
| Uniform::new(r.start, r.end) | ||
| } | ||
| } |
| /// range `[low, high]` (inclusive). Panics if `low > high`. | ||
| pub fn new_inclusive(low: X, high: X) -> Uniform<X> { | ||
| assert!(low < high, "Uniform::new called with `low >= high`"); | ||
| assert!(low <= high, "Uniform::new_inclusive called with `low > high`"); |
There was a problem hiding this comment.
Good idea to solve #403. I looked over the code and don't see any issues with low == high.
There was a problem hiding this comment.
Yes. low == high is nonsensical, but not impossible.
There was a problem hiding this comment.
It is the trivial case, but but it is well-defined and supporting it might simplify more general code.
|
Not sure why the comments from my previous review are "outdated"; there still appear to be some spurious uses of |
|
I rebased on master. |
|
@vks could you fix the tests please? |
* `gen_range` was not changed. * The types are still available under their deprecated, old names. Fixes rust-random#393.
|
Sorry again about that, something went wrong during the rebase (I already fixed those problems during the merge). Fixed and rebased so the whole history passes the tests. |
dhardy
left a comment
There was a problem hiding this comment.
So other than a couple of very minor things I'm ready to see this merged. @pitdicker are you happy with it now?
| /// sampling only a limited number of values from range. The default | ||
| /// implementation just sets up a range with `RangeImpl::new` and samples | ||
| /// Via this method, implementations can provide a method optimized for | ||
| /// sampling only a limited number of values from the range. The default |
There was a problem hiding this comment.
I guess this should say "for sampling only a single value from the specified range", but not actually a problem with your PR.
| /// repeatedly with the same arguments, one should use `Uniform`, as | ||
| /// that will amortize the computations that allow for perfect | ||
| /// uniformity, as they only happen when constructing the `Range`. | ||
| /// uniformity, as they only happen when constructing the `Uniform`. |
There was a problem hiding this comment.
I agree with @pitdicker actually that a couple of things could be changed:
- "wrapper around
distributions::Uniform::sample_single" - "when constructing the
Uniformrange."
Yes, feel free to merge when ready 😄 |
|
Okay, I made the final doc changes myself. |
|
Thanks! |
gen_rangewas not changed.Fixes #393.