-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Tracking issue for {min,max} on iterators #27724
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
Haskell uses |
Used |
Nominating for discussion in 1.6 |
I'm in favor of stabilizing something here, but don't want to keep the |
Agree with @aturon. |
🔔 This issue is now entering its cycle-long final comment period for stabilization 🔔 The libs team discussed this and the conclusion was to stabilize this via one of two routes:
We're a little up in the air about which of these should be done, but I would personally lean towards option (1) to have the closure take two arguments. |
I like the existing semantics better, because it reduces how many times the comparison keys must be computed, in case that's an expensive thing to produce. But I can also appreciate the lifetime issues of trying to key interior references. Can we just have both flavors? ( |
@cuviper We discussed having both flavors at the libs meeting, and there is general support for the idea, but I worry that there will be a paper cut around which of |
One note brought up by @huonw is that we could also have something like: struct Rank2Compare<F>(F);
impl<A, B, F> FnMut(A, A) -> Ordering for Rank2Compare<F>
where F: FnMut(A) -> B, B: Ord
{ /* ... */ }
iter.min_by(|a, b| a.field1.cmp(&b.field1));
iter.min_by(Rank2Compare(|a| &a.field1)); Which is to say that the "compare a and b" function is the most general, so we could build up "min by via a ranking function" on top with a special comparator. Note that the name "Rank2Compare" is pretty awful, and there may not be a fantastic name for this :) |
How about "Rank" is weird to me here, but maybe I'm just used to Python's |
@cuviper |
Haskell calls One downside to the |
I agree that |
I agree that we should have |
This was discussed in the libs team triage today and the decision was to stabilize with the |
After our team discussion, I was feeling a bit suspicious about the signature: fn min_by<B, F>(self, f: F) -> Option<Self::Item> where B: Ord, F: FnMut(&Self::Item) -> B As @alexcrichton points out in the original issue, it's not obvious that you can return interior references here (e.g. projecting out a component of a struct), because the return type And, in fact, this is largely the case -- although in some cases an iterator over references is able to do such a projection: fn main() {
let v = vec![(0, vec![0, 1])];
// this is OK -- the use of `iter` means there's a layer of reference
// internally whose lifetime you can use for the output reference.
v.iter().min_by(|v| &v.1);
// for some reason, this doesn't work:
// v.iter().min_by(|v| v);
// v.iter().min_by(|v| &*v);
// this is not OK, no internal reference:
// v.into_iter().min_by(|v| &v.1);
} This is unfortunate, but I think should not block stabilization; we just don't have a way to allow both extracting interior references and computing new owned values. Perhaps with HKT there will be a backcompat way to add this functionality later. |
v.iter().min_by(|v| a);
v.iter().min_by(|v| &*a); the type of I don't know how to deal with the example |
The methods |
This is a tracking issue for the unstable
iter_cmp
feature in the standard library. This is the ability to get the min or max elements out of an iterator via a custom closure for comparisons.Some open questions are:
_by
the right suffix here? The stable<[T]>::sort_by
function has a different signature than these two functions, so these may want to change suffix to something likemin_with
.&A
toB
? Lifetimes could perhaps get in the way where interior references toA
could not be returned (e.g. comparing by just theString
name of a person, not their height as well).The text was updated successfully, but these errors were encountered: