-
Notifications
You must be signed in to change notification settings - Fork 13.3k
min_by requires Ord not PartialOrd #17648
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
Comments
It wouldn't return the correct results for floating point types. It requires |
Maybe we can do the same for let ints: &[i32] = [-3, 0, 1, 5, -10];
assert_eq!(*ints.iter().min_by(|x, y| x.abs().cmp(&y.abs())).unwrap(), 0);
let floats: &[f64] = [-3., 0., 1., 5., -10.];
assert_eq!(*floats.iter().min_by(|x, y| x.abs().partial_cmp(&y.abs()).unwrap()).unwrap(), 0.); |
Sure, that's likely a saner definition. cc #15311 |
It’s not clear to me that If however the values are known not to be #[deriving(PartialOrd, PartialEq, Clone)]
struct NotNan<T> {
value: T
}
impl<T> NotNan<T> where T: Float {
fn new(value: T) -> Option<NotNan<T>> {
if value.is_nan() {
None
} else {
Some(NotNan { value: value })
}
}
}
impl<T> Eq for NotNan<T> where T: Float {}
impl<T> Ord for NotNan<T> where T: Float {
fn cmp(&self, other: &NotNan) -> Ordering {
let &NotNan(a) = self;
let &NotNan(b) = other;
if a == b {
Equal
} else if a > b {
Greater
} else {
Less
}
}
} CC @aturon |
Haskell's sortBy (comparing len) There's actually another way to do this: combine the basic sortBy (compare `on` len)
|
I don't see why we should add Haskell's If we also add a strings.iter().min_by(comparing(str::len)) Which is not much more complicated than what we have to do today. And |
I agree that |
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized. This issue has been moved to the RFCs repo: rust-lang/rfcs#675 |
It would be handy if Iterator::min_by required PartialOrd instead of Ord. This requirement makes it impossible to use with among other common types f32 and f64, which is annoying. I don't know if the best solution is to simply change the required trait to PartialOrd or to provide some other method min_by_partial_ord or something along those lines. The same reasoning should apply to Iterator::max_by
The text was updated successfully, but these errors were encountered: