-
Notifications
You must be signed in to change notification settings - Fork 1
/
any.go
49 lines (45 loc) · 1.27 KB
/
any.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package utils
import (
"errors"
"reflect"
)
// Any applys a function(the first parameter) returning a boolean value to each element of a slice(second parameter), if there exist at least one element make that function return true then Any will return true, otherwise false.
// Notice that Any return a boolean value NOT an interface{}
func Any(function, slice interface{}) (ret bool, err error) {
defer getErr(&err)
ret = any(function, slice)
return
}
func any(function, slice interface{}) bool {
in := reflect.ValueOf(slice)
if in.Kind() != reflect.Slice {
newErr(errors.New("The first param is not a slice"), "Any")
}
fn := reflect.ValueOf(function)
inType := in.Type().Elem()
if !verifyAnyFuncType(fn, inType) {
newErr(errors.New("Function must be of type func("+inType.String()+") bool"), "Any")
}
var param [1]reflect.Value
out := false
for i := 0; i < in.Len(); i++ {
param[0] = in.Index(i)
if fn.Call(param[:])[0].Bool() {
out = true
break
}
}
return out
}
func verifyAnyFuncType(fn reflect.Value, elemType reflect.Type) bool {
if fn.Kind() != reflect.Func {
return false
}
if fn.Type().NumIn() != 1 || fn.Type().NumOut() != 1 {
return false
}
if fn.Type().In(0) != elemType || fn.Type().Out(0).Kind() != reflect.Bool {
return false
}
return true
}