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

Methods to get max and min keys #20

Open
IgnisDa opened this issue Aug 21, 2024 · 3 comments
Open

Methods to get max and min keys #20

IgnisDa opened this issue Aug 21, 2024 · 3 comments

Comments

@IgnisDa
Copy link

IgnisDa commented Aug 21, 2024

I would like to get the key with the maximum and minimum number of duplicates. To do that with the current API, I have to convert it to a vec and use max_by. It would be great if HashBag had this functionality build in.

@TylerBloom
Copy link
Contributor

Does this not work? Any internal implementation of min/max would likely work roughly the same.

bag
  .set_iter()
  .fold((usize::MAX, usize::MIN), |(min, max), val| (std::cmp::min(min, val), std::cmp::max(max, val)))

@IgnisDa
Copy link
Author

IgnisDa commented Aug 21, 2024

Yeah this is what I want, but as an internal method.

Edit: The example you gave does not compile.

@TylerBloom
Copy link
Contributor

Sorry, I wrote that example on my phone. The correct example would be this:

bag
  .set_iter()
  .fold((usize::MAX, usize::MIN), |(min, max), (_, val)| (std::cmp::min(min, val), std::cmp::max(max, val)))

That said, my point was that you don't need to copy everything into a Vec if you only want the min and/or max. If the above example is along the lines of what you are looking for, consider opening a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants