-
Notifications
You must be signed in to change notification settings - Fork 11
Add new iter_counts method #23
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
base: master
Are you sure you want to change the base?
Add new iter_counts method #23
Conversation
The `iter` method returns an iterator that returns each key the appropriate number of times. The new `iter_counts` method returns an iterator that returns tuples that contain the key and the number of times it appears.
|
If one wants |
|
I ran into this crate when I was trying to keep track of the factors for a number. Conceptually, the order didn't matter and I wanted constant-time lookup for the factors. However, there also needed to be duplicates. I could have constructed the However, I then needed to loop through the number of times each factor appeared, and so I needed an I'll admit that it may not be the most common occurrence, but I think that it would be nice to have. I'm not in love with the naming scheme, but it was the best I could come up with at the time and seemed to make sense to me... |
|
The python multiset also also implements an equivalent to |
| /// keys_with_counts.sort(); // Order is arbitrary | ||
| /// assert_eq!(keys_with_counts, vec![(&0, 2), (&1, 1)]); | ||
| /// ``` | ||
| pub fn iter_counts(&self) -> IterCounts<K> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be done with less code. Something like this should work:
pub fn iter_counts(&self) -> impl Iterator<Item = (&K, usize)> {
self.elem_counts.iter().map(|(key, &counts)| (key, counts))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's true. The general practice for the standard library has been to provide a concrete struct to return. However, returning an impl trait is a relatively recent change, so that may be considered a new best practice. I'd be willing to go that way. It may be worth considering for the sake of consistency, since we do need custom structs for several of the other methods (i.e. UnionCounts, etc.).
|
I forgot, we already have a
Never mind. With the Well, considering the solution is fairly simple, I would be ok with not having this. It would be consistent with the The equivalent approach for anyone who Googles this in the future: multiset.distinct_elements().map(|elem| (elem, multiset.count_of(elem))) |
|
Hi. |
The
itermethod returns an iterator that returns each key the appropriate number of times. The newiter_countsmethod returns an iterator that returns tuples that contain the key and the number of times it appears.This implements another feature referenced in #17.
Let me know what you think of the name/whatever.