-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Added minmax function. #11789
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
Added minmax function. #11789
Conversation
@@ -882,6 +882,15 @@ pub trait OrdIterator<A> { | |||
/// assert!(a.iter().min().unwrap() == &1); | |||
/// ``` | |||
fn min(&mut self) -> Option<A>; | |||
|
|||
/// `minmax` finds the mininum and maximum elements in the vector. | |||
/// The return type `MinMaxResult` is an enum of three variants. |
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.
Could you put an empty line before this sentence? rustdoc will take the first paragraph as the summary, and I think just the first line is appropriate.
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.
Also, it would be worth mentioning that this is more efficient that just min
and max
, since it does 1.5 comparisions on average.
And, these 3 points could be a markdown list:
The return type `MinMaxResult` is an enum of three variants:
- `NoElements` is returned ...
- `OneElement(x)` is returned ...
- ...
Fixed comments by @huonw . |
@@ -882,6 +882,39 @@ pub trait OrdIterator<A> { | |||
/// assert!(a.iter().min().unwrap() == &1); | |||
/// ``` | |||
fn min(&mut self) -> Option<A>; | |||
|
|||
/// `min_max` finds the mininum and maximum elements in the vector. |
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.
s/vector/iterator/
What is the value in using a custom enum with a state for |
That requires |
Fair enough. Perhaps |
Maybe, I'm personally not particularly fussed either way. Also, @pongad, there appears to be a merge commit in the list of commits, it would be good to remove it before merging this PR (rebasing & force pushing is perfectly ok). |
@pongad I was suggesting having impl<T: Clone> MinMaxResult<T> {
fn into_option(self) -> Option<(T, T)> {
match self {
NoElements => None,
OneElement(x) => Some((x.clone(), x)),
MinMax(a, b) => Some((a, b))
}
}
} |
Some(x) => x | ||
}; | ||
let second = match self.next() { | ||
None => {left_over = Some(first); break;} |
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.
Why not move there the code in "match left_over" and eliminate the left_over variable entirely?
That would slightly shorten the code.
@bill-myers We don't know the length of the iterator, so we don't know ahead of time if we need the left_over or not. |
You can inline the let second = match self.next() {
None => {
if first < min {
min = first
} else if first > max {
max = first
}
break;
}
} and just avoid using Also, you appear to have git troubles: github is telling me that there are 238 changed files. (I've found it easier to just avoid merging entirely, rather than merging and fixing it up later. This also requires avoiding |
@huonw I see it now. Sorry, I completely misunderstood. I just fixed the git problem (I think). I'll make the changes and test, will push when ready. |
Fixed comments from @huonw |
/// - `NoElements` if the iterator is empty. | ||
/// - `OneElement(x)` if the iterator has exactly one element. | ||
/// - `MinMax(x, y)` is returned otherwise, where `x <= y`. Two values are equal if and only if | ||
/// there are more than one elements in the iterator and all elements are equal. |
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 think "are more than one elements" is more grammatically correct as "is more than one element".
Implementation looks good. Thanks for |
r=kballard |
Though, I'm not entirely sure why into_option uses self and not &self. Compilation doesn't like &self, and I'm not quite sure why :( |
@pongad Taking |
Tests ok
@kballard I'm not "fluent" with the way Rust handles borrowing yet, but I think I see what you are saying. That would mean after calling |
@pongad Yes, |
All tests passing. #5268