hashbag icon indicating copy to clipboard operation
hashbag copied to clipboard

Methods to get max and min keys

Open IgnisDa opened this issue 1 year ago • 3 comments

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.

IgnisDa avatar Aug 21 '24 17:08 IgnisDa

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)))

TylerBloom avatar Aug 21 '24 20:08 TylerBloom

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

Edit: The example you gave does not compile.

IgnisDa avatar Aug 21 '24 23:08 IgnisDa

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.

TylerBloom avatar Sep 15 '24 17:09 TylerBloom