Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upTracking issue for {min,max} on iterators #27724
Comments
alexcrichton
added
A-iterators
T-libs
B-unstable
labels
Aug 12, 2015
This comment has been minimized.
This comment has been minimized.
Haskell uses |
Ms2ger
referenced this issue
Aug 16, 2015
Open
Tracking: Unstable Rust feature gates used by Servo #5286
This comment has been minimized.
This comment has been minimized.
|
Used |
This comment has been minimized.
This comment has been minimized.
|
Nominating for discussion in 1.6 |
alexcrichton
added
the
I-nominated
label
Nov 4, 2015
This comment has been minimized.
This comment has been minimized.
|
I'm in favor of stabilizing something here, but don't want to keep the |
This comment has been minimized.
This comment has been minimized.
|
Agree with @aturon. |
This comment has been minimized.
This comment has been minimized.
|
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. |
alexcrichton
added
final-comment-period
and removed
I-nominated
labels
Nov 5, 2015
This comment has been minimized.
This comment has been minimized.
|
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? ( |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
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 :) |
This comment has been minimized.
This comment has been minimized.
|
How about "Rank" is weird to me here, but maybe I'm just used to Python's |
This comment has been minimized.
This comment has been minimized.
|
@cuviper |
This comment has been minimized.
This comment has been minimized.
|
Haskell calls One downside to the |
This comment has been minimized.
This comment has been minimized.
|
I agree that |
This comment has been minimized.
This comment has been minimized.
|
I agree that we should have |
This comment has been minimized.
This comment has been minimized.
|
This was discussed in the libs team triage today and the decision was to stabilize with the |
This comment has been minimized.
This comment has been minimized.
|
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) -> BAs @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. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
|
The methods |
alexcrichton commentedAug 12, 2015
This is a tracking issue for the unstable
iter_cmpfeature 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:
_bythe right suffix here? The stable<[T]>::sort_byfunction has a different signature than these two functions, so these may want to change suffix to something likemin_with.&AtoB? Lifetimes could perhaps get in the way where interior references toAcould not be returned (e.g. comparing by just theStringname of a person, not their height as well).