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