1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add ContainBy function

This commit is contained in:
dudaodong
2023-02-24 14:19:10 +08:00
parent e523d4af6e
commit 9d2c9806d1
5 changed files with 152 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ import (
- [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain)
- [ContainBy](#ContainBy)
- [ContainSubSlice](#ContainSubSlice)
- [Chunk](#Chunk)
- [Compact](#Compact)
@@ -151,6 +152,51 @@ func main() {
}
```
### <span id="ContainBy">ContainBy</span>
<p>returns true if predicate function return true.</p>
<b>Signature:</b>
```go
func ContainBy[T any](slice []T, predicate func(item T) bool) bool
```
<b>Example:</b>
```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
}
```
### <span id="ContainSubSlice">ContainSubSlice</span>
<p>Check if the slice contain subslice or not.</p>

View File

@@ -24,6 +24,7 @@ import (
- [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain)
- [ContainBy](#ContainBy)
- [ContainSubSlice](#ContainSubSlice)
- [Chunk](#Chunk)
- [Compact](#Compact)
@@ -151,6 +152,51 @@ func main() {
}
```
### <span id="ContainBy">ContainBy</span>
<p>根据predicate函数判断切片是否包含某个值。</p>
<b>函数签名:</b>
```go
func ContainBy[T any](slice []T, predicate func(item T) bool) bool
```
<b>示例:</b>
```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
}
```
### <span id="ContainSubSlice">ContainSubSlice</span>
<p>判断slice是否包含subslice</p>

View File

@@ -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 {

View File

@@ -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"})

View File

@@ -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"}))