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

Update mergeMaps making it more flexible #248

Closed
wants to merge 3 commits into from
Closed

Update mergeMaps making it more flexible #248

wants to merge 3 commits into from

Conversation

bambinoua
Copy link

The updated version of mergeMaps function allows to merge maps with removal functionality.

final map1 = {'a': 'a'};
final map2 = {'b': 'b'};
final map3 = {'c': 'c'};
final map12 = mergeMaps(map1, map2);                     // {'a': 'a', 'b': 'b'}
final map123 = mergeMaps(map12, map3);                   // {'a': 'a', 'b': 'b', 'c': 'c'}
final map123_reduced1 = mergeMaps(map123, {'c': null});  // {'a': 'a', 'b': 'b'}
final map4 = {'a': 'a', 'b': 'b'};
final map123_reduced2 = mergeMaps(map123, map4, 
 removeDifference: true);                                // {'a': 'a', 'b': 'b'}

Improve `mergeMaps` function to make it more flexible.
Add additional comments
Fix bug with `removeDifference` functionality when `value` is null
@natebosch
Copy link
Member

This new behavior seems pretty specialized. I don't think we should have behavior dependent on treating null value in some special way - it's not typical to treat present with a null value differently from an absent key, but we give it a special meaning here it will be breaking for anyone that is doing it already.

The removeDifference could also be handled outside this method mergeMaps(aMap, bMap)..removeWhere((k, v) => !bMap.containsKey(k)). With that behavior it's also feasible to write it with a map literal { for (var MapEntry(:key, :value) in bMap.entries) key: aMap.containsKey(key) ? _merge(aMap[key], value) : value}

I don't think it's worth making mergeMaps more complicated for this. cc @lrhn in case you have different preferences.

@natebosch natebosch closed this May 17, 2023
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.

None yet

2 participants