mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
feat: add ContainBy function
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
|||||||
|
|
||||||
- [AppendIfAbsent](#AppendIfAbsent)
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
|
- [ContainBy](#ContainBy)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
- [Chunk](#Chunk)
|
- [Chunk](#Chunk)
|
||||||
- [Compact](#Compact)
|
- [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>
|
### <span id="ContainSubSlice">ContainSubSlice</span>
|
||||||
|
|
||||||
<p>Check if the slice contain subslice or not.</p>
|
<p>Check if the slice contain subslice or not.</p>
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
|
|
||||||
- [AppendIfAbsent](#AppendIfAbsent)
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
|
- [ContainBy](#ContainBy)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
- [Chunk](#Chunk)
|
- [Chunk](#Chunk)
|
||||||
- [Compact](#Compact)
|
- [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>
|
### <span id="ContainSubSlice">ContainSubSlice</span>
|
||||||
|
|
||||||
<p>判断slice是否包含subslice</p>
|
<p>判断slice是否包含subslice</p>
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Contain check if the target value is in the slice or not.
|
// 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 {
|
func Contain[T comparable](slice []T, target T) bool {
|
||||||
for _, item := range slice {
|
for _, item := range slice {
|
||||||
if item == target {
|
if item == target {
|
||||||
@@ -33,6 +33,17 @@ func Contain[T comparable](slice []T, target T) bool {
|
|||||||
return false
|
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.
|
// ContainSubSlice check if the slice contain a given subslice or not.
|
||||||
// Play: https://go.dev/play/p/bcuQ3UT6Sev
|
// Play: https://go.dev/play/p/bcuQ3UT6Sev
|
||||||
func ContainSubSlice[T comparable](slice, subSlice []T) bool {
|
func ContainSubSlice[T comparable](slice, subSlice []T) bool {
|
||||||
|
|||||||
@@ -19,6 +19,32 @@ func ExampleContain() {
|
|||||||
// false
|
// 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() {
|
func ExampleContainSubSlice() {
|
||||||
result1 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "b"})
|
result1 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "b"})
|
||||||
result2 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "d"})
|
result2 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "d"})
|
||||||
|
|||||||
@@ -20,6 +20,28 @@ func TestContain(t *testing.T) {
|
|||||||
assert.Equal(true, Contain([]int{1, 2, 3}, 1))
|
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) {
|
func TestContainSubSlice(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestContainSubSlice")
|
assert := internal.NewAssert(t, "TestContainSubSlice")
|
||||||
assert.Equal(true, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "a"}))
|
assert.Equal(true, ContainSubSlice([]string{"a", "a", "b", "c"}, []string{"a", "a"}))
|
||||||
|
|||||||
Reference in New Issue
Block a user