Skip to content
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

Tracking issue for slice::sort_by_key #30423

Closed
alexcrichton opened this issue Dec 16, 2015 · 11 comments
Closed

Tracking issue for slice::sort_by_key #30423

alexcrichton opened this issue Dec 16, 2015 · 11 comments
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@alexcrichton
Copy link
Member

As-is on the tin, mirrors the recently-stabilized min_by_key and max_by_key methods on Iterator.

@alexcrichton alexcrichton added I-nominated T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. B-unstable Blocker: Implemented in the nightly compiler and unstable. labels Dec 16, 2015
@alexcrichton
Copy link
Member Author

🔔 This issue is now entering its final comment period for stabilization 🔔

@alexcrichton alexcrichton added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed I-nominated labels Dec 17, 2015
@malbarbo
Copy link
Contributor

There is a function related to sort_by_key that is missing: binary_search_by_key.

sort -> binary_search
sort_by -> binary_search_by
sort_by_key -> binary_search_by_key

@alexcrichton
Copy link
Member Author

An excellent point! I'll see if I can't add that soon to sneak it in for this FCP as well

@alexcrichton
Copy link
Member Author

Hm actually, now that I think about it I'm not quite sure what the signature for binary_search_by_key would look like. I'm not sure it jives well with the other patterns (e.g. it's "just a change in closure arguments"), as I think it needs to look something like:

fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
    where F: FnOnce(&Self::Item) -> B, B: Ord;

Is that what you were thinking as well?

@malbarbo
Copy link
Contributor

Yes.

I suggest renaming B to K (also in sort_by_key) and b to key.

@alexcrichton
Copy link
Member Author

Hm this may actually be a case where I'm not sure the extra method may buy much, for example compare:

slice.binary_search_by(|k| k.key.cmp(&val));

// vs

slice.binary_search_by_key(&val, |k| &k.key);

The Ordering-based one is not only shorter by one character but it also looks a little more readable to me. Along those lines I'd actually be hesitant to add it just yet

@malbarbo
Copy link
Contributor

For search with closures that maybe the case, but for search with methods or functions binary_search_by_key is more readable

s.sort_by_key(Point::x);
// this is more readable
s.binary_search_by_key(&1, Point::x);
// than this
s.binary_search_by(|p| p.x().cmp(&1));

@alexcrichton
Copy link
Member Author

Oh hm yeah, good point!

@alexcrichton
Copy link
Member Author

The libs team discussed this recently and the decision was to stabilize

@alexcrichton
Copy link
Member Author

Closed by #30943

@malbarbo
Copy link
Contributor

Should we open another issue for binary_search_by_key or you think it's no relevant anymore?

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Apr 12, 2016
This method adds to the family of `_by_key` methods, and is the
counterpart of `slice::sort_by_key`. It was mentioned on rust-lang#30423 but
was not implemented at that time.

Refs rust-lang#30423
bors added a commit that referenced this issue Apr 15, 2016
collections: Add slice::binary_search_by_key

This method adds to the family of `_by_key` methods, and is the
counterpart of `slice::sort_by_key`. It was mentioned on #30423 but
was not implemented at that time.

Refs #30423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants