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

feat: add Some and Every function in slice.go

This commit is contained in:
dudaodong
2021-12-09 19:27:20 +08:00
parent 8c8f991390
commit 188d52cd9d
2 changed files with 71 additions and 3 deletions

View File

@@ -89,6 +89,50 @@ func Difference(slice1, slice2 interface{}) interface{} {
return res.Interface()
}
// Every return true if all of the values in the slice pass the predicate function.
// The function signature should be func(index int, value interface{}) bool .
func Every(slice, function interface{}) bool {
sv := sliceValue(slice)
fn := functionValue(function)
elemType := sv.Type().Elem()
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
panic("Filter function must be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
}
var indexes []int
for i := 0; i < sv.Len(); i++ {
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
if flag.Bool() {
indexes = append(indexes, i)
}
}
return len(indexes) == sv.Len()
}
// Some return true if any of the values in the list pass the predicate function.
// The function signature should be func(index int, value interface{}) bool .
func Some(slice, function interface{}) bool {
sv := sliceValue(slice)
fn := functionValue(function)
elemType := sv.Type().Elem()
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
panic("Filter function must be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
}
var indexes []int
for i := 0; i < sv.Len(); i++ {
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
if flag.Bool() {
indexes = append(indexes, i)
}
}
return len(indexes) > 0
}
// Filter iterates over elements of slice, returning an slice of all elements `signature` returns truthy for.
// The function signature should be func(index int, value interface{}) bool .
func Filter(slice, function interface{}) interface{} {

View File

@@ -96,15 +96,39 @@ func TestConvertSlice(t *testing.T) {
//}
}
func TestEvery(t *testing.T) {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
res := Every(nums, isEven)
if res != false {
utils.LogFailedTestInfo(t, "Every", nums, false, res)
t.FailNow()
}
}
func TestSome(t *testing.T) {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
res := Some(nums, isEven)
if res != true {
utils.LogFailedTestInfo(t, "Some", nums, true, res)
t.FailNow()
}
}
func TestFilter(t *testing.T) {
s1 := []int{1, 2, 3, 4, 5}
nums := []int{1, 2, 3, 4, 5}
even := func(i, num int) bool {
return num%2 == 0
}
e1 := []int{2, 4}
r1 := Filter(s1, even)
r1 := Filter(nums, even)
if !reflect.DeepEqual(r1, e1) {
utils.LogFailedTestInfo(t, "Filter", s1, e1, r1)
utils.LogFailedTestInfo(t, "Filter", nums, e1, r1)
t.FailNow()
}