Skip to content

Stabilize slice_sort_by_cached_key#58074

Merged
bors merged 2 commits intorust-lang:masterfrom
scottmcm:stabilize-sort_by_cached_key
Feb 17, 2019
Merged

Stabilize slice_sort_by_cached_key#58074
bors merged 2 commits intorust-lang:masterfrom
scottmcm:stabilize-sort_by_cached_key

Conversation

@scottmcm
Copy link
Member

@scottmcm scottmcm commented Feb 2, 2019

I was going to ask on the tracking issue (#34447), but decided to just send this and hope for an FCP here. The method was added last March by #48639.

Signature: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_cached_key

impl [T] {
    pub fn sort_by_cached_key<K, F>(&mut self, f: F)
        where F: FnMut(&T) -> K, K: Ord;
}

That's an identical signature to the existing sort_by_key, so I think the questions are just naming, implementation, and the usual "do we want this?".

The implementation seems to have proven its use in rustc at least, which many uses: https://github.com/rust-lang/rust/search?l=Rust&q=sort_by_cached_key

(I'm asking because it's exactly what I just needed the other day:

    all_positions.sort_by_cached_key(|&n|
        data::CITIES.iter()
            .map(|x| *metric_closure.get_edge(n, x.pos).unwrap())
            .sum::<usize>()
    );

since caching that key is a pretty obviously good idea.)

Closes #34447

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking issue for sorting by expensive-to-compute keys (feature slice_sort_by_cached_key)

8 participants