Skip to content

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

Open
@Lasering

Description

@Lasering

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions