-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilter.go
More file actions
184 lines (153 loc) · 4.4 KB
/
filter.go
File metadata and controls
184 lines (153 loc) · 4.4 KB
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
package async
import (
"reflect"
)
/*
Filter allows you to filter out information from a slice in Waterfall mode.
You must call the Done function with false as its first argument if you do
not want the data to be present in the results. No other arguments will
affect the performance of this function. When calling the Done function,
an error will cause the filtering to immediately exit.
For example, take a look at one of the tests for this function:
func TestFilterString(t *testing.T) {
str := []string{
"test1",
"test2",
"test3",
"test4",
"test5",
}
expects := []string{
"test1",
"test2",
"test4",
"test5",
}
mapper := func(done async.Done, args ...interface{}) {
Status("Hit string")
Status("Args: %+v\n", args)
if args[0] == "test3" {
done(nil, false)
return
}
done(nil, true)
}
final := func(err error, results ...interface{}) {
Status("Hit string end")
Status("Results: %+v\n", results)
for i := 0; i < len(results); i++ {
if results[i] != expects[i] {
t.Errorf("Did not filter correctly.")
break
}
}
}
async.Filter(str, mapper, final)
}
Each Routine function will be passed the current value and its index the
slice for its arguments.
*/
func Filter(data interface{}, routine Routine, callbacks ...Done) {
var (
routines []Routine
results []interface{}
)
d := reflect.ValueOf(data)
for i := 0; i < d.Len(); i++ {
v := d.Index(i).Interface()
routines = append(routines, func(id int) Routine {
return func(done Done, args ...interface{}) {
done = func(original Done) Done {
return func(err error, args ...interface{}) {
if args[0] != false {
results = append(results, v)
}
if id == (d.Len() - 1) {
original(err, results...)
return
}
original(err, args...)
}
}(done)
routine(done, v, id)
}
}(i))
}
Waterfall(routines, callbacks...)
}
/*
FilterParallel allows you to filter out information from a slice in Parallel
mode.
You must call the Done function with false as its first argument if you do
not want the data to be present in the results. No other arguments will
affect the performance of this function.
If there is an error, any further results will be discarded but it will not
immediately exit. It will continue to run all of the other Routine functions
that were passed into it. This is because by the time the error is sent, the
goroutines have already been started. At this current time, there is no way
to cancel a sleep timer in Go.
For example, take a look at one of the tests for this function:
func TestFilterStringParallel(t *testing.T) {
str := []string{
"test1",
"test2",
"test3",
"test4",
"test5",
}
expects := []string{
"test1",
"test2",
"test4",
"test5",
}
mapper := func(done async.Done, args ...interface{}) {
Status("Hit string")
Status("Args: %+v\n", args)
if args[0] == "test3" {
done(nil, false)
return
}
done(nil, true)
}
final := func(err error, results ...interface{}) {
Status("Hit string end")
Status("Results: %+v\n", results)
for i := 0; i < len(results); i++ {
if results[i] != expects[i] {
t.Errorf("Did not filter correctly.")
break
}
}
}
async.FilterParallel(str, mapper, final)
}
Each Routine function will be passed the current value and its index the
slice for its arguments.
The output of filtering in Parallel mode cannot be guaranteed to stay in the
same order, due to the fact that it may take longer to process some things
in your filter routine. If you need the data to stay in the order it is in,
use Filter instead to ensure it stays in order.
*/
func FilterParallel(data interface{}, routine Routine, callbacks ...Done) {
var routines []Routine
d := reflect.ValueOf(data)
for i := 0; i < d.Len(); i++ {
v := d.Index(i).Interface()
routines = append(routines, func(id int) Routine {
return func(done Done, args ...interface{}) {
done = func(original Done) Done {
return func(err error, args ...interface{}) {
if args[0] != false {
original(err, v)
return
}
original(err)
}
}(done)
routine(done, v, id)
}
}(i))
}
Parallel(routines, callbacks...)
}