1
0
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:
dudaodong
2023-02-24 14:19:10 +08:00
parent e523d4af6e
commit 9d2c9806d1
5 changed files with 152 additions and 1 deletions
+12 -1
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 {