Skip to content

Commit 5cd2181

Browse files
committed
feat(sliceutils): add ContainsAll and Duplicate functions
This change adds the following functions to the sliceutils package. Unit tests are also provided. `ContainsAll[T comparable](target []T, values []T) bool` returns true if the given target slice of values contains all the given values, otherwise it returns false. `Duplicate[T any](src []T) []T` returns a newly allocated copy of the given slice. Signed-off-by: m-d-key <[email protected]>
1 parent a30233c commit 5cd2181

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

pkg/sliceutils/sliceutils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ func CollectErrors[T any](root error, elements ...result.Result[T]) error {
8989
return nil
9090
}
9191

92+
// ContainsAll returns true if the given target slice of values contains all the
93+
// given values, otherwise it returns false.
94+
func ContainsAll[T comparable](target []T, values []T) bool {
95+
for _, value := range values {
96+
if !slices.Contains(target, value) {
97+
return false
98+
}
99+
}
100+
return true
101+
}
102+
92103
// Detect returns the first value found in the given slice of values that matches
93104
// the given filter. Returns a maybe.Nothing if no value is found. Returns a
94105
// result.Error if the filter fails.
@@ -180,6 +191,13 @@ func Disjoint[T comparable](left []T, right []T) result.Result[[]T] {
180191
return UniqueUnion(leftOnly.MustGet(), rightOnly.MustGet())
181192
}
182193

194+
// Duplicate returns a newly allocated copy of the given slice.
195+
func Duplicate[T any](src []T) []T {
196+
dst := make([]T, len(src))
197+
_ = copy(dst, src)
198+
return dst
199+
}
200+
183201
// FirstError returns the first error, if any, in the given slice of
184202
// result.Result.
185203
func FirstError[T any](elements ...result.Result[T]) error {

pkg/sliceutils/sliceutils_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ func Test_CollectErrors_no_elements(t *testing.T) {
9393
require.NoError(t, err)
9494
}
9595

96+
func Test_ContainsAll(t *testing.T) {
97+
haystack := []string{"one", "two", "three", "four", "five"}
98+
presentNeedles := []string{"one", "three", "four"}
99+
absentNeedles1 := []string{"one", "two", "three", "four", "five", "six"}
100+
absentNeedles2 := []string{"one", "six"}
101+
require.True(t, sliceutils.ContainsAll(haystack, presentNeedles))
102+
require.False(t, sliceutils.ContainsAll(haystack, absentNeedles1))
103+
require.False(t, sliceutils.ContainsAll(haystack, absentNeedles2))
104+
}
105+
96106
func Test_Detect_error(t *testing.T) {
97107
expectedMessage := `failed DETECT filter`
98108
filter := newFailingFilter[string](expectedMessage)
@@ -283,6 +293,13 @@ func Test_Disjoint_empty(t *testing.T) {
283293
require.Empty(t, actual.MustGet())
284294
}
285295

296+
func Test_Duplicate(t *testing.T) {
297+
src := []int{1, 2, 3}
298+
dst := sliceutils.Duplicate(src)
299+
require.Equal(t, src, dst)
300+
require.NotSame(t, &src[0], &dst[0])
301+
}
302+
286303
func Test_FirstError(t *testing.T) {
287304
errs := []result.Result[string]{
288305
result.Ok("ok"),

0 commit comments

Comments
 (0)