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

implement diff for map #2569

Closed
carnott-snap opened this issue Mar 22, 2020 · 6 comments
Closed

implement diff for map #2569

carnott-snap opened this issue Mar 22, 2020 · 6 comments
Labels

Comments

@carnott-snap
Copy link

Set::diff is really helpful for calculating set difference, unfortunately, unlike Guava which has Maps.difference, there is nothing in Map.

@mincong-h
Copy link
Member

Hello @carnott-snap , do you know if there is Maps.difference or similar methods in Scala? I did a quick search on https://github.com/scala/scala but I didn't find any. I think the goal of VAVR is not to compare with Guava, but to align with Scala as much as possible.

@carnott-snap
Copy link
Author

I do not have experience with the scala stdlib, and was not aware vavr was tracking the scala stdlib. I just the need for this transform, and somebody commented that this exists in guava, so I started there. Do you think the feature has merit on its own, or is sticking to the scala api more important?

@mincong-h
Copy link
Member

mincong-h commented May 13, 2020

I am probably not the right person to make the decision so I will let core maintainers to expression their ideas. Meanwhile, I hope the following code snippet could unblock you for map usage. (I never used Guava so I hope I didn't make any mistake):

// Guava Maps#difference() equivalence in VAVR 1.0.0-alpha-3

Map<String, String> mapA = HashMap.of("a1", "1", "c1", "1", "c2", "2");
Map<String, String> mapB = HashMap.of("b1", "1", "c1", "1", "c2", "3");

// MapDifference#areEqual
boolean areEqual = mapA.equals(mapB);
assertThat(areEqual).isFalse();
assertThat(mapA.equals(HashMap.of("a1", "1", "c1", "1", "c2", "2"))).isTrue();

// MapDifference#entriesOnlyOnLeft
Map<String, String> entriesOnlyOnLeft = mapA.removeAll(mapB.keySet());
assertThat(entriesOnlyOnLeft).isEqualTo(HashMap.of("a1", "1"));

// MapDifference#entriesOnlyOnRight
Map<String, String> entriesOnlyOnRight = mapB.removeAll(mapA.keySet());
assertThat(entriesOnlyOnRight).isEqualTo(HashMap.of("b1", "1"));

// MapDifference#entriesInCommon
Map<String, String> entriesInCommon = mapA.retainAll(mapB);
assertThat(entriesInCommon).isEqualTo(HashMap.of("c1", "1"));

// MapDifference#entriesDiffering
// no, there is not... :/

@carnott-snap
Copy link
Author

slick, I was just going to use the guava logic and convert everything, but this is probably better. Plus I can just directly upstream the logic if that is the route you end up taking.

Assuming we do, you think the interface should be like guava, where a complex object that can be interrogated is returned, or more building block methods like left.difference(right), right.difference(left), left.union(right), mapping to your entriesOnlyOnLeft, entriesOnlyOnRight, and entriesInCommon respectively?

@urandom2
Copy link

did this get implemented, or you are closing because it is stale?

@danieldietrich
Copy link
Contributor

Yes, @mincong-h suggestion is the way to go. Java has mutable collections, so Guava's Maps.difference makes sense. In Vavr, collections are persistent and we have a lot more operations. Removing elements will create a new version, so it is a safe alternative to Guavas utils.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants