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

feat: Allow other types with use of generics in collections module #1343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions modules/collections/lists.go
Expand Up @@ -2,8 +2,8 @@ package collections

// ListIntersection returns all the items in both list1 and list2. Note that this will dedup the items so that the
// output is more predictable. Otherwise, the end list depends on which list was used as the base.
func ListIntersection(list1 []string, list2 []string) []string {
out := []string{}
func ListIntersection[T comparable](list1 []T, list2 []T) []T {
out := []T{}

// Only need to iterate list1, because we want items in both lists, not union.
for _, item := range list1 {
Expand All @@ -16,8 +16,8 @@ func ListIntersection(list1 []string, list2 []string) []string {
}

// ListSubtract removes all the items in list2 from list1.
func ListSubtract(list1 []string, list2 []string) []string {
out := []string{}
func ListSubtract[T comparable](list1 []T, list2 []T) []T {
out := []T{}

for _, item := range list1 {
if !ListContains(list2, item) {
Expand All @@ -29,7 +29,7 @@ func ListSubtract(list1 []string, list2 []string) []string {
}

// ListContains returns true if the given list of strings (haystack) contains the given string (needle).
func ListContains(haystack []string, needle string) bool {
func ListContains[T comparable](haystack []T, needle T) bool {
for _, str := range haystack {
if needle == str {
return true
Expand Down