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

Set method that returns all areas of a set intersection #184

Open
Lasering opened this issue Jun 26, 2024 · 0 comments
Open

Set method that returns all areas of a set intersection #184

Lasering opened this issue Jun 26, 2024 · 0 comments

Comments

@Lasering
Copy link

Lasering commented Jun 26, 2024

When computing the set differences its very common to need access to all "areas" of a set intersection:

val existingLocationIds: Set[String] = ???
val newLocationIds: Set[String] = ???
val (removed, unchanged, added) = existingLocationIds.fullIntersection(newLocationIds)

This can be easily implemented with:

val unchanged = existingLocationsIds.intersect(newLocationIds)
val removed = existingLocationsIds.diff(unchanged)
val added = newLocationIds.diff(unchanged)

However this is not very performant, as we are iterating each set multiple times. Thus a performant method could be implemented. The name fullIntersection is just a placeholder, I'm sure there is a better name for this.

As an added bonus we could even take this one step further and have a fullIntersectionBy variant. In most cases this would be much more useful:

val existingLocations: Seq[Location] = ???
val newLocations: Seq[Location] = ???
val (removed, unchanged, added) = existingLocations.toSet.fullIntersectionBy(newLocations.toSet)(_.id)

Since List also has the intersect method we could also add it directly there:

existingLocations.fullIntersectionBy(newLocations)(_.id)
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

No branches or pull requests

1 participant