1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 15:12:26 +08:00

feat: add EqualWithFunc function for slice

This commit is contained in:
dudaodong
2022-06-13 14:15:32 +08:00
parent 0cbb3dd97e
commit ef2d8e14b0
2 changed files with 29 additions and 0 deletions

View File

@@ -162,6 +162,22 @@ func Equal[T comparable](slice1, slice2 []T) bool {
return true
}
// EqualWithFunc checks if two slices are equal with comparator func
func EqualWithFunc[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool {
if len(slice1) != len(slice2) {
return false
}
for i, v1 := range slice1 {
v2 := slice2[i]
if !comparator(v1, v2) {
return false
}
}
return true
}
// Every return true if all of the values in the slice pass the predicate function.
func Every[T any](slice []T, predicate func(index int, item T) bool) bool {
if predicate == nil {