From d39e8cb3ef9c88f79cd8696d36cf6c5295b0e29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20D=C3=ADaz?= Date: Tue, 19 Sep 2023 16:17:51 -0300 Subject: [PATCH] feat: Allow other types wiht use of generics in collections module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Agustín Díaz --- modules/collections/lists.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/collections/lists.go b/modules/collections/lists.go index ec468d380..113998e60 100644 --- a/modules/collections/lists.go +++ b/modules/collections/lists.go @@ -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 { @@ -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) { @@ -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