diff --git a/README.md b/README.md index 25b85cf..fd7f9c7 100644 --- a/README.md +++ b/README.md @@ -1465,6 +1465,9 @@ import "github.com/duke-git/lancet/v2/slice" - **ContainSubSlice** : check if the slice contain a given subslice or not. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#ContainSubSlice)] [[play](https://go.dev/play/p/bcuQ3UT6Sev)] +- **ContainAny** : check if the slice contains any element from the targets slice. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#ContainAny)] + [[play](https://go.dev/play/p/4xoxhc9XSSw)] - **Chunk** : creates a slice of elements split into groups the length of size. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Chunk)] [[play](https://go.dev/play/p/b4Pou5j2L_C)] diff --git a/README_zh-CN.md b/README_zh-CN.md index a6a1f5b..5f8f311 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -1471,6 +1471,9 @@ import "github.com/duke-git/lancet/v2/slice" - **ContainSubSlice** : 判断 slice 是否包含 subslice。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#ContainSubSlice)] [[play](https://go.dev/play/p/bcuQ3UT6Sev)] +- **ContainAny** : 判断 slice 是否包含 targets 切片中的任意一个元素。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#ContainAny)] + [[play](https://go.dev/play/p/4xoxhc9XSSw)] - **Chunk** : 按照 size 参数均分 slice。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Chunk)] [[play](https://go.dev/play/p/b4Pou5j2L_C)] diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 7787c3d..e2d0053 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -27,6 +27,7 @@ import ( - [Contain](#Contain) - [ContainBy](#ContainBy) - [ContainSubSlice](#ContainSubSlice) +- [ContainAny](#ContainAny) - [Chunk](#Chunk) - [Compact](#Compact) - [Concat](#Concat) @@ -256,6 +257,43 @@ func main() { } ``` +### ContainAny + +
判断slice是否包含targets切片中的任意一个元素
+ +函数签名: + +```go +func ContainAny[T comparable](slice []T, targets []T) bool +``` + +示例:[运行](https://go.dev/play/p/4xoxhc9XSSw) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.ContainAny([]string{"a", "b", "c"}, []string{"a"}) + result2 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "e"}) + result3 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "a"}) + result4 := slice.ContainAny([]string{"a", "b", "c"}, []string{}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // false + // true + // false +} +``` + ### Chunk按照size参数均分slice
diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 243981c..b674e0f 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -27,6 +27,7 @@ import ( - [Contain](#Contain) - [ContainBy](#ContainBy) - [ContainSubSlice](#ContainSubSlice) +- [ContainAny](#ContainAny) - [Chunk](#Chunk) - [Compact](#Compact) - [Concat](#Concat) @@ -256,6 +257,43 @@ func main() { } ``` +### ContainAny + +Check if the slice contains any element from the targets slice.
+ +Signature: + +```go +func ContainAny[T comparable](slice []T, targets []T) bool +``` + +Example:[Run](https://go.dev/play/p/4xoxhc9XSSw) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.ContainAny([]string{"a", "b", "c"}, []string{"a"}) + result2 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "e"}) + result3 := slice.ContainAny([]string{"a", "b", "c"}, []string{"d", "a"}) + result4 := slice.ContainAny([]string{"a", "b", "c"}, []string{}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // false + // true + // false +} +``` + ### ChunkCreates an slice of elements split into groups the length of `size`.
diff --git a/slice/slice.go b/slice/slice.go index 34624dd..632afcf 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -74,6 +74,27 @@ func ContainSubSlice[T comparable](slice, subSlice []T) bool { return true } +// ContainAny check if the slice contains any element from the targets slice. +// Play: https://go.dev/play/p/4xoxhc9XSSw +func ContainAny[T comparable](slice []T, targets []T) bool { + if len(targets) == 0 { + return false + } + + sliceMap := make(map[T]struct{}, len(slice)) + for _, item := range slice { + sliceMap[item] = struct{}{} + } + + for _, target := range targets { + if _, exists := sliceMap[target]; exists { + return true + } + } + + return false +} + // Chunk creates a slice of elements split into groups the length of size. // Play: https://go.dev/play/p/b4Pou5j2L_C func Chunk[T any](slice []T, size int) [][]T { diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index 29643eb..f028e0b 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -58,6 +58,24 @@ func ExampleContainSubSlice() { // false } +func ExampleContainAny() { + result1 := ContainAny([]string{"a", "b", "c"}, []string{"a"}) + result2 := ContainAny([]string{"a", "b", "c"}, []string{"d", "e"}) + result3 := ContainAny([]string{"a", "b", "c"}, []string{"d", "a"}) + result4 := ContainAny([]string{"a", "b", "c"}, []string{}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // false + // true + // false +} + func ExampleChunk() { arr := []string{"a", "b", "c", "d", "e"} diff --git a/slice/slice_test.go b/slice/slice_test.go index 0107fc5..e91c9c6 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -89,6 +89,46 @@ func TestContainSubSlice(t *testing.T) { } } +func TestContainAny(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestContainAny") + + tests := []struct { + slice []string + targets []string + want bool + }{ + {[]string{"a", "b", "c"}, []string{"a"}, true}, + {[]string{"a", "b", "c"}, []string{"a", "b"}, true}, + {[]string{"a", "b", "c"}, []string{"d", "e"}, false}, + {[]string{"a", "b", "c"}, []string{"d", "a"}, true}, + {[]string{"a", "b", "c"}, []string{}, false}, + {[]string{}, []string{"a"}, false}, + {[]string{}, []string{}, false}, + {[]string{"a", "b", "c"}, []string{"c", "d", "e"}, true}, + } + + for _, tt := range tests { + assert.Equal(tt.want, ContainAny(tt.slice, tt.targets)) + } + + intTests := []struct { + slice []int + targets []int + want bool + }{ + {[]int{1, 2, 3, 4, 5}, []int{3}, true}, + {[]int{1, 2, 3, 4, 5}, []int{6, 7}, false}, + {[]int{1, 2, 3, 4, 5}, []int{5, 6, 7}, true}, + {[]int{1, 2, 3, 4, 5}, []int{}, false}, + } + + for _, tt := range intTests { + assert.Equal(tt.want, ContainAny(tt.slice, tt.targets)) + } +} + func TestChunk(t *testing.T) { t.Parallel()