-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement new method HashMap.insert_or_update_with() #6815
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
Implement new method HashMap.insert_or_update_with() #6815
Conversation
Another implementation of this could be:
But your way is much more elegant. |
That said, why do you return the value instead of a Other than this question, this request looks good to me. |
I actually don't care about the return value at all, but I returned it for parity with |
Of course, |
@kballard and I talked things through on IRC, but we didn't come up with a good conclusion. Haskell's Data.Map has a couple similar functions, namely insertWith and insertLookupWithKey. A slight variation on this function could cover all the use cases of Haskell's functions:
where the first argument of the
Which is a little uglier. I don't think this is that bad. What does everyone else think? |
After thinking about this some more, one worry I have with the new functions is it won't let me modify the existing value, instead I have to create a brand new one. So I can't say something like
|
I think the most general pattern is this:
and we should add that, and then we can build |
This code, incidentally, becomes:
|
I guess I have no strong opinion about what mangle should return, perhaps |
and the proper name is probably |
@nikomatsakis: Building Two alternatives I can see are either to have |
Also, presumably it should be |
AFAIK I can't actually build
complains (rightly) that |
Looks like I can do the job using |
A couple of comments:
in which case you can write find_or_insert as follows:
|
I wasn't sure if you intended for it to be public or private. I'm happy making it public. |
Add new private hashmap function fn mangle(&mut self, k: K, not_found: &fn(&K) -> V, found: &fn(&K, &mut V)) -> uint Rewrite find_or_insert() and find_or_insert_with() on top of mangle(). Also take the opportunity to change the return type of find_or_insert() and find_or_insert_with() to &'a mut V. This fixes rust-lang#6394.
fn insert_or_update_with<'a>(&'a mut self, k: K, f: &fn(&K, &mut V)) -> &'a V
The latest commits make the suggested changes. |
@kballard: Awesome, thanks for doing this! |
…erickt `std::hashmap::HashMap.insert_or_update_with()` is basically the opposite of `find_or_insert_with()`. It inserts a given key-value pair if the key does not already exist, or replaces the existing value with the output of the passed function if it does. This is useful because replicating this with existing functionality is awkward, especially with the current borrow-checker. In my own project I have code that looks like if match map.find_mut(&key) { None => { true } Some(x) => { *x += 1; false } } { map.insert(key, 0); } and it took several iterations to make it look this good. The new function turns this into map.insert_or_update_with(key, 0, |_,x| *x += 1);
…logiq search_is_some: add checking for `is_none()` fixes: rust-lang#6815 changelog: search_is_some: add checking for `is_none()`. To be honest I don't know what is the process of renaming the lints. Appreciate any feedback if that needs to be handled differently. Thanks!
std::hashmap::HashMap.insert_or_update_with()
is basically the oppositeof
find_or_insert_with()
. It inserts a given key-value pair if the keydoes not already exist, or replaces the existing value with the output
of the passed function if it does.
This is useful because replicating this with existing functionality is awkward, especially with the current borrow-checker. In my own project I have code that looks like
and it took several iterations to make it look this good. The new function turns this into