-
Notifications
You must be signed in to change notification settings - Fork 6
/
any.go
65 lines (54 loc) · 1.86 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package godash
import (
"fmt"
"reflect"
)
// Any checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy.
// Currently, input of type slice is supported
//
// Validations:
//
// 1. Predicate function should take one argument and return one value
// 2. Predicate function should return a bool value
// 3. Predicate function's argument should be of the same type as the elements of the input slice
//
// Validation errors are returned to the caller
func Any(in, predicateFn interface{}) (bool, error) {
var output bool
input := reflect.ValueOf(in)
predicate := reflect.ValueOf(predicateFn)
if predicate.Kind() != reflect.Func {
return output, fmt.Errorf("predicateFn has to be a function")
}
predicateFnType := predicate.Type()
if predicateFnType.NumIn() != 1 {
return output, fmt.Errorf("predicate function has to take only one argument")
}
if predicateFnType.NumOut() != 1 {
return output, fmt.Errorf("predicate function should return only one return value")
}
if predicateFnType.Out(0).Kind() != reflect.Bool {
return output, fmt.Errorf("predicate function should return a boolean value")
}
inputKind := input.Kind()
if inputKind == reflect.Slice {
inputSliceElemType := input.Type().Elem
predicateFnArgType := predicateFnType.In(0)
if inputSliceElemType() != predicateFnArgType {
return output, fmt.Errorf("predicate function's argument (%s) has to be (%s)", predicateFnArgType, inputSliceElemType())
}
for i := 0; i < input.Len(); i++ {
arg := input.Index(i)
returnValue := predicate.Call([]reflect.Value{arg})[0]
if returnValue.Bool() {
return true, nil
}
}
return output, nil
}
return output, fmt.Errorf("not implemented for (%s)", inputKind)
}
// Some is an alias for Any function
func Some(in, predicateFn interface{}) (bool, error) {
return Any(in, predicateFn)
}