diff --git a/docs/slice.md b/docs/slice.md index acdd7e3b..05261769 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -24,6 +24,7 @@ import ( - [AppendIfAbsent](#AppendIfAbsent) - [Contain](#Contain) +- [ContainBy](#ContainBy) - [ContainSubSlice](#ContainSubSlice) - [Chunk](#Chunk) - [Compact](#Compact) @@ -151,6 +152,51 @@ func main() { } ``` +### ContainBy + +

returns true if predicate function return true.

+ +Signature: + +```go +func ContainBy[T any](slice []T, predicate func(item T) bool) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + type foo struct { + A string + B int + } + + array1 := []foo{{A: "1", B: 1}, {A: "2", B: 2}} + result1 := slice.ContainBy(array1, func(f foo) bool { return f.A == "1" && f.B == 1 }) + result2 := slice.ContainBy(array1, func(f foo) bool { return f.A == "2" && f.B == 1 }) + + array2 := []string{"a", "b", "c"} + result3 := slice.ContainBy(array2, func(t string) bool { return t == "a" }) + result4 := slice.ContainBy(array2, func(t string) bool { return t == "d" }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // false + // true + // false +} +``` + ### ContainSubSlice

Check if the slice contain subslice or not.

diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index d9838f9b..45ff24b7 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -24,6 +24,7 @@ import ( - [AppendIfAbsent](#AppendIfAbsent) - [Contain](#Contain) +- [ContainBy](#ContainBy) - [ContainSubSlice](#ContainSubSlice) - [Chunk](#Chunk) - [Compact](#Compact) @@ -151,6 +152,51 @@ func main() { } ``` +### ContainBy + +

根据predicate函数判断切片是否包含某个值。

+ +函数签名: + +```go +func ContainBy[T any](slice []T, predicate func(item T) bool) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + type foo struct { + A string + B int + } + + array1 := []foo{{A: "1", B: 1}, {A: "2", B: 2}} + result1 := slice.ContainBy(array1, func(f foo) bool { return f.A == "1" && f.B == 1 }) + result2 := slice.ContainBy(array1, func(f foo) bool { return f.A == "2" && f.B == 1 }) + + array2 := []string{"a", "b", "c"} + result3 := slice.ContainBy(array2, func(t string) bool { return t == "a" }) + result4 := slice.ContainBy(array2, func(t string) bool { return t == "d" }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // false + // true + // false +} +``` + ### ContainSubSlice

判断slice是否包含subslice

diff --git a/slice/slice.go b/slice/slice.go index 6ea2f546..98c79a66 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -22,7 +22,7 @@ var ( ) // Contain check if the target value is in the slice or not. -// Play: https://go.dev/play/p/_454yEHcNjf +// Play: todo func Contain[T comparable](slice []T, target T) bool { for _, item := range slice { if item == target { @@ -33,6 +33,17 @@ func Contain[T comparable](slice []T, target T) bool { return false } +// ContainBy returns true if predicate function return true. +func ContainBy[T any](slice []T, predicate func(item T) bool) bool { + for _, item := range slice { + if predicate(item) { + return true + } + } + + return false +} + // ContainSubSlice check if the slice contain a given subslice or not. // Play: https://go.dev/play/p/bcuQ3UT6Sev func ContainSubSlice[T comparable](slice, subSlice []T) bool { diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index 045e846a..0adf1ae9 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -19,6 +19,32 @@ func ExampleContain() { // false } +func ExampleContainBy() { + type foo struct { + A string + B int + } + + array1 := []foo{{A: "1", B: 1}, {A: "2", B: 2}} + result1 := ContainBy(array1, func(f foo) bool { return f.A == "1" && f.B == 1 }) + result2 := ContainBy(array1, func(f foo) bool { return f.A == "2" && f.B == 1 }) + + array2 := []string{"a", "b", "c"} + result3 := ContainBy(array2, func(t string) bool { return t == "a" }) + result4 := ContainBy(array2, func(t string) bool { return t == "d" }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // false + // true + // false +} + func ExampleContainSubSlice() { result1 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "b"}) result2 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "d"}) diff --git a/slice/slice_test.go b/slice/slice_test.go index 99ee415e..6dc1e82e 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -20,6 +20,28 @@ func TestContain(t *testing.T) { assert.Equal(true, Contain([]int{1, 2, 3}, 1)) } +func TestContainBy(t *testing.T) { + assert := internal.NewAssert(t, "TestContainBy") + + type foo struct { + A string + B int + } + + array1 := []foo{{A: "1", B: 1}, {A: "2", B: 2}} + result1 := ContainBy(array1, func(f foo) bool { return f.A == "1" && f.B == 1 }) + result2 := ContainBy(array1, func(f foo) bool { return f.A == "2" && f.B == 1 }) + + array2 := []string{"a", "b", "c"} + result3 := ContainBy(array2, func(t string) bool { return t == "a" }) + result4 := ContainBy(array2, func(t string) bool { return t == "d" }) + + assert.Equal(true, result1) + assert.Equal(false, result2) + assert.Equal(true, result3) + assert.Equal(false, result4) +} + func TestContainSubSlice(t *testing.T) { assert := internal.NewAssert(t, "TestContainSubSlice") assert.Equal(true, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "a"}))