Skip to content

Allow Union/Intersect to take many lists#181

Merged
samber merged 10 commits intosamber:masterfrom
OpenSourceProjects:intersect
Nov 5, 2025
Merged

Allow Union/Intersect to take many lists#181
samber merged 10 commits intosamber:masterfrom
OpenSourceProjects:intersect

Conversation

@frankywahl
Copy link
Contributor

@frankywahl frankywahl commented Jul 13, 2022

Allow a union & intersect of any number of lists.

I also thought about changing the API to for more flexibility:

func Union[T comparable](lists ...[]T) []T 
func Intersect[T comparable](lists ...[]T) []T 

This would be backward compatible, but would change the required number of arguments, so left it as it was for now. Happy to change it.


Update: since the PR was opened, Union was changed to take a variadic list. So I've updated this PR to have the same method signature.

@frankywahl frankywahl changed the title Allow Union to take many lists Allow Union/Intersect to take many lists Jul 13, 2022
@frankywahl
Copy link
Contributor Author

@samber Any updates on this, I've updated it recently given that Union has been implemented since the PR was open.

intersect.go Outdated
Comment on lines +117 to +125
for _, elem := range lists[len(lists)-1] {
if _, ok := seen[elem]; ok {
result = append(result, elem)
}
}

return result
lists[0] = result

return Intersect(lists[:len(lists)-1]...)
Copy link
Owner

@samber samber Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't perform recursion since it will require extra computation

for i := 1; i < len(lists); i++ {
    for j := 0; j < len(lists[i]); j++ {
		if _, ok := seen[lists[i][j]]; !ok {
			delete(seen, lists[i][j])
		}
	}
    if len(seen) == 0 {
        return Slice{}
    }
}

return Keys(seen)

@samber
Copy link
Owner

samber commented Nov 5, 2025

Please add an example in lo_example.go and update documentation in README.md and docs/

@samber samber merged commit ed8ee74 into samber:master Nov 5, 2025
@NathanBaulch
Copy link
Contributor

NathanBaulch commented Nov 6, 2025

This breaks CI due to a minor linting issue @samber @frankywahl. Do CI failures not block PR merges?
I only mention this because it causes all new PRs to fail CI.

@frankywahl frankywahl mentioned this pull request Nov 6, 2025
@frankywahl
Copy link
Contributor Author

frankywahl commented Nov 6, 2025

This breaks CI due to a minor linting issue @samber @frankywahl. Do CI failures not block PR merges? I only mention this because it causes all new PRs to fail CI.

Sorry about this, fixed it in #731

@samber
Copy link
Owner

samber commented Nov 6, 2025

@frankywahl i usually don't block PRs with linting issues to avoid round-trip and delays for merge. And sometimes, I forget to apply the fix on the master branch 😅

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.

3 participants