-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathany.go
67 lines (54 loc) · 1.45 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
66
67
package un
import (
"reflect"
)
func init() {
MakeAny(&Any)
MakeAny(&AnyInt)
MakeAny(&AnyString)
}
// Any func(func(A, bool), bool)
// Returns true if all values in the collection (slice or map) pass the predicate truth test
// var Any func(func(value interface{}) bool, interface{}) bool
var Any func(fn, slice_or_map interface{}) bool
// AnyInt
// Returns true if all values in a []int pass the predicate truth test
// Predicate function accepts an int and returns a boolean
var AnyInt func(func(value int) bool, []int) bool
// AnyString
// Returns true if all values in a []string pass the predicate truth test
// Predicate function accepts a string and returns a boolean
var AnyString func(func(value string) bool, []string) bool
// MakeAny: implements a typed Each function in the form Each func(func(A, B), []A)
func MakeAny(fn interface{}) {
Maker(fn, any)
}
func any(values []reflect.Value) []reflect.Value {
fn, col := extractArgs(values)
var ret bool
if col.Kind() == reflect.Map {
ret = anyMap(fn, col)
}
if col.Kind() == reflect.Slice {
ret = anySlice(fn, col)
}
return Valueize(reflect.ValueOf(ret))
}
func anySlice(fn, s reflect.Value) bool {
for i := 0; i < s.Len(); i++ {
v := s.Index(i)
if ok := callPredicate(fn, v); ok {
return true
}
}
return false
}
func anyMap(fn, m reflect.Value) bool {
for _, k := range m.MapKeys() {
v := m.MapIndex(k)
if ok := callPredicate(fn, v); ok {
return true
}
}
return false
}