-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatmap.go
More file actions
48 lines (42 loc) · 895 Bytes
/
flatmap.go
File metadata and controls
48 lines (42 loc) · 895 Bytes
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
package flame
/**************************/
// FlatMapper
/**************************/
type FlatMapNode[X, Y any] struct {
Input chan X
Outputs []chan Y
Proc func(X) []Y
}
func AddFlatMapper[X, Y any](w *Workflow, f func(X) []Y) Node[X, Y] {
n := &FlatMapNode[X, Y]{Proc: f, Outputs: []chan Y{}}
w.Nodes = append(w.Nodes, n)
return n
}
func (n *FlatMapNode[X, Y]) start(wf *Workflow) {
wf.WaitGroup.Add(1)
go func() {
if n.Input != nil {
for x := range n.Input {
y := n.Proc(x)
for i := range n.Outputs {
for _, z := range y {
n.Outputs[i] <- z
}
}
}
}
for i := range n.Outputs {
close(n.Outputs[i])
}
wf.WaitGroup.Done()
}()
}
func (n *FlatMapNode[X, Y]) GetOutput() chan Y {
m := make(chan Y)
n.Outputs = append(n.Outputs, m)
return m
}
func (n *FlatMapNode[X, Y]) Connect(e Emitter[X]) {
o := e.GetOutput()
n.Input = o
}