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

Comments

Projects
None yet
2 participants
@alexcrichton
Copy link
Member

alexcrichton commented Dec 16, 2015

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

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Dec 17, 2015

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

@malbarbo

This comment has been minimized.

Copy link
Contributor

malbarbo commented Dec 18, 2015

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

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Dec 18, 2015

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

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Dec 18, 2015

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

This comment has been minimized.

Copy link
Contributor

malbarbo commented Dec 18, 2015

Yes.

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

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Dec 18, 2015

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

This comment has been minimized.

Copy link
Contributor

malbarbo commented Dec 19, 2015

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

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Dec 19, 2015

Oh hm yeah, good point!

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Jan 15, 2016

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

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Jan 21, 2016

Closed by #30943

@malbarbo

This comment has been minimized.

Copy link
Contributor

malbarbo commented Feb 15, 2016

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 3, 2016

collections: Add slice::binary_search_by_key
This method adds to the family of `_by_key` method, 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

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Apr 3, 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 rust-lang#30423 but
was not implemented at that time.

Refs rust-lang#30423

kamalmarhubi added a commit to kamalmarhubi/rust that referenced this issue Apr 12, 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 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

Auto merge of #32693 - kamalmarhubi:binary_search_by_key, r=alexcrichton
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
You can’t perform that action at this time.