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

Ordering::and_then #34774

Closed
digama0 opened this issue Jul 12, 2016 · 4 comments
Closed

Ordering::and_then #34774

digama0 opened this issue Jul 12, 2016 · 4 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@digama0
Copy link
Contributor

digama0 commented Jul 12, 2016

This seems like an obvious and missing function from std::cmp::Ordering:

impl Ordering {
    pub fn then(self, other: Ordering) -> Ordering {
        match self {
            Ordering::Equal => other,
            o => o,
        }
    }

    pub fn and_then<F: FnOnce() -> Ordering>(self, other: F) -> Ordering {
        match self {
            Ordering::Equal => other(),
            o => o,
        }
    }
}

This is useful for creating comparators which do lexicographic order. The second variant allows for skipping the order calculation if it is not necessary. Names are of course open to bikeshedding.

@steveklabnik steveklabnik added A-libs C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 12, 2016
@nagisa
Copy link
Member

nagisa commented Jul 14, 2016

Seems like a weird name for such a function IMO. Also, I believe, this should go into the RFC repo.

@digama0
Copy link
Contributor Author

digama0 commented Jul 14, 2016

@nagisa Java has a similar function for comparators, called thenComparing. The idea is that you can read a comparison function like

self.name.cmp(other.name).then(self.address.cmp(other.address))

as "compare by name, then by address". The then / and_then difference with a closure is inspired by Rust's function names for Option like or (by value) / or_else (by closure).

I don't know about the RFC thing, I usually see library functions posted here. Plus, I'm not confident I can handle the politics of RFC -> feature -> stabilization, so I am hoping someone more prominent thinks this is a good idea and picks up the baton.

@nagisa
Copy link
Member

nagisa commented Jul 14, 2016

I don't know about the RFC thing, I usually see library functions posted here. Plus, I'm not confident I can handle the politics of RFC -> feature -> stabilization, so I am hoping someone more prominent thinks this is a good idea and picks up the baton.

You don’t need a full RFC, only this issue should be in that repository, since this issue (as well as many other feature requests) are RFCs in their most primitive form.

The idea is that you can read a comparison function like

When you put it like this it seems to make sense, but I feel like there still may be better ways to call it, especially for and_then variation.

@apasel422
Copy link
Contributor

This was implemented in #37054 with #37053 as the tracking issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants