-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
111 lines (91 loc) · 2.19 KB
/
helpers.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
package datachan
import (
"log"
"reflect"
)
// FromStringSlice returns a stage seeded with strings
// in the array.
func FromStringSlice(input []string) *Stage {
output := make(chan string)
go func() {
for _, v := range input {
output <- v
}
close(output)
}()
return Source(output)
}
// Pipe sends the output of the previous stage to a channel,
// and optionally closes the channel after the transmision is done.
//
// This is useful for nesting datachan flows, for example, within
// a Map operation.
func (s *Stage) Pipe(output T, close bool) {
value := reflect.ValueOf(output)
if value.Kind() != reflect.Chan {
panic("Pipe argument must be a channel")
}
for v, ok := s.output.Recv(); ok; v, ok = s.output.Recv() {
value.Send(v)
}
if close {
value.Close()
}
}
// LogCompletion logs when the previous stage finished. Useful for logging or debugging
func (s *Stage) LogCompletion(name string, bufferSize int) *Stage {
output := reflect.MakeChan(s.output.Type(), bufferSize)
go func() {
for v, ok := s.output.Recv(); ok; v, ok = s.output.Recv() {
output.Send(v)
}
log.Println("Finished stage", name)
output.Close()
}()
return &Stage{output}
}
// Count returns the count of elements entering from previous stage. It is best used with Tee.
func (s *Stage) Count() <-chan uint {
output := make(chan uint)
go func() {
count := uint(0)
for _, ok := s.output.Recv(); ok; _, ok = s.output.Recv() {
count++
}
output <- count
close(output)
}()
return output
}
// Drop drops all the elements from previous stage. It returns the same type as input.
func (s *Stage) Drop(bufferSize int) *Stage {
output := reflect.MakeChan(s.output.Type(), bufferSize)
go func() {
for _, ok := s.output.Recv(); ok; _, ok = s.output.Recv() {
}
output.Close()
}()
return &Stage{output}
}
func (s *Stage) Limit(limit int) *Stage {
if limit < 0 {
return s
}
output := reflect.MakeChan(s.output.Type(), limit)
go func() {
i := 0
for value, ok := s.output.Recv(); ok; value, ok = s.output.Recv() {
if i < limit {
i++
output.Send(value)
} else if i == limit {
output.Close()
i++
}
}
if i < limit {
output.Close()
}
}()
return &Stage{output}
}