Skip to content

Commit

Permalink
feat: add ContainBy function
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Feb 24, 2023
1 parent e523d4a commit 9d2c980
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
46 changes: 46 additions & 0 deletions docs/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

- [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain)
- [ContainBy](#ContainBy)
- [ContainSubSlice](#ContainSubSlice)
- [Chunk](#Chunk)
- [Compact](#Compact)
Expand Down Expand Up @@ -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>
Expand Down
46 changes: 46 additions & 0 deletions docs/slice_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

- [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain)
- [ContainBy](#ContainBy)
- [ContainSubSlice](#ContainSubSlice)
- [Chunk](#Chunk)
- [Compact](#Compact)
Expand Down Expand Up @@ -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>
Expand Down
13 changes: 12 additions & 1 deletion slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions slice/slice_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
22 changes: 22 additions & 0 deletions slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
Expand Down

0 comments on commit 9d2c980

Please sign in to comment.