Skip to content

Commit

Permalink
Improve arrays API
Browse files Browse the repository at this point in the history
  • Loading branch information
mpraski committed Sep 16, 2022
1 parent 0f07469 commit 3748c68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 15 additions & 4 deletions arrays/arrays.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package arrays

func Contains[T comparable](s []T, e T) bool {
func Contains[T comparable](s []T, e ...T) bool {
for _, a := range s {
if a == e {
return true
for _, b := range e {
if a == b {
return true
}
}
}

Expand All @@ -12,7 +14,16 @@ func Contains[T comparable](s []T, e T) bool {

func Subset[T comparable](s []T, e ...T) bool {
for _, a := range e {
if !Contains(s, a) {
var c bool

for _, b := range s {
if a == b {
c = true
break
}
}

if !c {
return false
}
}
Expand Down
9 changes: 6 additions & 3 deletions arrays/arrays_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package arrays_test

import (
"strings"
"testing"

"github.com/blue-health/blue-go-toolbox/arrays"
Expand Down Expand Up @@ -95,9 +96,11 @@ func TestSubset(t *testing.T) {
}

for _, c := range cases {
if arrays.Subset(c.super, c.sub...) != c.result {
t.Fatalf("Subset returned false bool. Superset: %v, Subset: %s, Expected: %t", c.super, c.sub, c.result)
}
t.Run(strings.Join(c.super, ",")+":"+strings.Join(c.sub, ","), func(t *testing.T) {
if arrays.Subset(c.super, c.sub...) != c.result {
t.Fatalf("Subset returned false bool. Superset: %v, Subset: %s, Expected: %t", c.super, c.sub, c.result)
}
})
}
}

Expand Down

0 comments on commit 3748c68

Please sign in to comment.