-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice_filter.go
212 lines (186 loc) · 5.62 KB
/
slice_filter.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package tricks
import "reflect"
func isValidMapFunc(funcType, sliceType reflect.Type) bool {
return funcType.NumIn() == 1 && funcType.NumOut() == 1 &&
funcType.In(0) == sliceType.Elem()
}
func isValidReduceFunc(funcType, sliceType reflect.Type) bool {
return funcType.NumIn() == 2 && funcType.NumOut() == 1 &&
funcType.In(0) == funcType.Out(0) &&
funcType.In(1) == sliceType.Elem()
}
func isValidBoolFunc(funcType, sliceType reflect.Type) bool {
return isValidMapFunc(funcType, sliceType) &&
funcType.Out(0).Kind() == reflect.Bool
}
// Any returns true if the given function returns true for any element in the
// slice. Otherwise, it returns false.
func (ts TrickSlice) Any(fn interface{}) bool {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidBoolFunc(f.Type(), v.Type()) {
panic("tricks: slice.Any: invalid function type")
}
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
if f.Call([]reflect.Value{val})[0].Bool() {
return true
}
}
return false
}
// All returns true if the given function returns true for every element in the
// slice. Otherwise, it returns false.
func (ts TrickSlice) All(fn interface{}) bool {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidBoolFunc(f.Type(), v.Type()) {
panic("tricks: slice.All: invalid function type")
}
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
if !f.Call([]reflect.Value{val})[0].Bool() {
return false
}
}
return true
}
// None returns true if the given function returns false for every element in the
// slice. Otherwise, it returns false.
func (ts TrickSlice) None(fn interface{}) bool {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidBoolFunc(f.Type(), v.Type()) {
panic("tricks: slice.None: invalid function type")
}
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
if f.Call([]reflect.Value{val})[0].Bool() {
return false
}
}
return true
}
// One returns true if the given function returns true for exactly one element
// in the slice. Otherwise, it returns false.
func (ts TrickSlice) One(fn interface{}) bool {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidBoolFunc(f.Type(), v.Type()) {
panic("tricks: slice.One: invalid function type")
}
found := false
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
if f.Call([]reflect.Value{val})[0].Bool() {
if found {
return false
}
found = true
}
}
return found
}
// Many returns true if the given function returns true for more than one element
// in the slice. Otherwise, it returns false.
func (ts TrickSlice) Many(fn interface{}) bool {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidBoolFunc(f.Type(), v.Type()) {
panic("tricks: slice.Many: invalid function type")
}
found := false
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
if f.Call([]reflect.Value{val})[0].Bool() {
if found {
return true
}
found = true
}
}
return false
}
// Filter returns a new slice, choosing only the elements for which the given
// function returns true.
func (ts TrickSlice) Filter(fn interface{}) TrickSlice {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidBoolFunc(f.Type(), v.Type()) {
panic("tricks: slice.Filter: invalid function type")
}
typ := v.Type()
out := reflect.MakeSlice(typ, 0, 0)
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
if f.Call([]reflect.Value{val})[0].Bool() {
out = reflect.Append(out, val)
}
}
return TrickSlice(out)
}
// Map applies the given function to each element of the slice and stores the
// result to a new slice. The cap() of the new slice is set to equal its length.
func (ts TrickSlice) Map(fn interface{}) TrickSlice {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidMapFunc(f.Type(), v.Type()) {
panic("tricks: slice.Map: invalid function type")
}
outType := f.Type().Out(0)
typ := reflect.SliceOf(outType)
out := reflect.MakeSlice(typ, v.Len(), v.Len())
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
result := f.Call([]reflect.Value{val})[0]
out.Index(i).Set(result)
}
return TrickSlice(out)
}
// Reduce applies the given function to the values of the slice and reduces them
// down to a single value. fn should be `func(X, T) X`, zero should be type X.
func (ts TrickSlice) Reduce(zero, fn interface{}) interface{} {
// TODO: Improve those docs above.
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidReduceFunc(f.Type(), v.Type()) {
panic("tricks: slice.Reduce: invalid function type")
}
outType := f.Type().Out(0)
z := reflect.ValueOf(zero)
if !z.IsValid() {
z = reflect.Zero(outType)
}
if z.Type() != outType {
panic("tricks: slice.Reduce: invalid zero type")
}
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
z = f.Call([]reflect.Value{z, val})[0]
}
return z.Interface()
}
// GroupBy collects the slice values into a map, where the keys are the return
// value of the grouping function and the values are slices of elements that
// correspond to that key.
func (ts TrickSlice) GroupBy(fn interface{}) TrickMap {
v := reflect.Value(ts)
f := reflect.ValueOf(fn)
if !f.IsValid() || !isValidMapFunc(f.Type(), v.Type()) {
panic("tricks: slice.GroupBy: invalid function type")
}
keyType := f.Type().Out(0)
valType := v.Type()
mapType := reflect.MapOf(keyType, valType)
out := reflect.MakeMap(mapType)
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
key := f.Call([]reflect.Value{val})[0]
group := out.MapIndex(key)
if !group.IsValid() {
group = reflect.MakeSlice(valType, 0, 1)
}
out.SetMapIndex(key, reflect.Append(group, val))
}
return TrickMap(out)
}