Skip to content

Add Itertools::into_group_map#258

Merged
bluss merged 4 commits into
rust-itertools:masterfrom
tobz1000:to_lookup
Jan 22, 2018
Merged

Add Itertools::into_group_map#258
bluss merged 4 commits into
rust-itertools:masterfrom
tobz1000:to_lookup

Conversation

@tobz1000
Copy link
Copy Markdown
Contributor

@tobz1000 tobz1000 commented Jan 12, 2018

Fixes #92.

Create a Lookup from an Iterator, in a similar manner to C#'s ToLookup.

Returns a plain HashMap, so no special FromIterator or .iter() implementations. My only concern is, if we do want to specialise this in future, it will require a new type and be a breaking change.

@bluss
Copy link
Copy Markdown
Member

bluss commented Jan 12, 2018

This is welcome but should be named so that it does not conflict or confuse with potential equivalents of collect to hashmap under names like into_hashmap.

Comment thread src/lib.rs Outdated
where Self: Iterator + Sized,
K: Hash + Eq,
F: Fn(&Self::Item) -> K
{
Copy link
Copy Markdown
Member

@bluss bluss Jan 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming aside (it is important too).

I would expect this to apply to iterators of pairs and use the first field as the key and the second as the value. Like HashMap::from_iter expects it. Then we don't have to worry about the diversity in calling mode required for extracting the key, be it by value (split the key out as a part of something) or by ref (clone/create a new key from a value).

This is a bit that we leave the model and conventions from C# behind (because it doesn't apply to Rust, we have ownership instead of ubiquitous reference taking), and we use the conventions and the model from Rust.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually thinking about just this :) haven't had much time to work on it this weekend but I was considering providing three different argument signatures:

  • iterator of pairs,
  • iterator with key function (i.e. the current version),
  • iterator with both key and value functions.

Seems reasonable enough to only provide the first one. However, my initial reasoning for doing it as it currently is, was to match .group_by(). I can't see any reason why this function and that one would take different arguments. Maybe .group_by() should take a pair iterator too?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, they seem to be in about the same situation. Maybe they differ in that the group by key is often not kept around.

It seems they should both do the same thing, and if I had thought of that, I'd have picked to mimic group_by as well.

@tobz1000
Copy link
Copy Markdown
Contributor Author

tobz1000 commented Jan 15, 2018

Changed name to to_group_lookup; not sure if it's what you had in mind. If not, let me know and I'll change by your suggestion; or you can change it yourself after merge.

Comment thread src/lib.rs Outdated
{
lookup::to_group_lookup(self)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an iterator "regular" method, not an adaptor, so it should be listed in next to the other methods.

@bluss
Copy link
Copy Markdown
Member

bluss commented Jan 21, 2018

Thanks. I'm going to suggest a name such as .into_group_hashmap() or .into_group_map(), then we don't need to invent a new (for Rust) word like lookup.

@bluss
Copy link
Copy Markdown
Member

bluss commented Jan 22, 2018

Edited; it's nice to say "Fulfills #92." but it is not what github prefers: https://help.github.com/articles/closing-issues-using-keywords/

@bluss
Copy link
Copy Markdown
Member

bluss commented Jan 22, 2018

Thanks for the solid work and thanks for using quickcheck tests.

@bluss bluss changed the title Add Itertools::to_lookup Add Itertools::into_group_map Jan 22, 2018
@bluss bluss merged commit 43fe1bf into rust-itertools:master Jan 22, 2018
@bluss
Copy link
Copy Markdown
Member

bluss commented Mar 4, 2018

This is finally released in itertools 0.7.7. Thanks, I think it's a very nice new feature.

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

Successfully merging this pull request may close these issues.

2 participants