-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
148 lines (137 loc) · 2.93 KB
/
map.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
package pipe
import "sync"
// Map takes message and converts it into another type by map function.
// If input channel is closed then output channel is closed.
// Creates a new channel with the same capacity as input.
//
// # Strategies
//
// - Processing: Parallel
// - Closing: Single
// - Capacity: Same
//
// # Usages
//
// // input := make(chan int, 4) with random values [1, 2, 3]
//
// output := Map(func(value int) string {
// fmt.Print(value)
// return fmt.Sprintf("val: %d", value)
// }, input)
//
// // stdout: 2 1 3
// // output: ["val: 2", "val: 1", "val: 3"]
func Map[Tin, Tout any](mapper func(Tin) Tout, in <-chan Tin) <-chan Tout {
out := make(chan Tout, cap(in))
wg := sync.WaitGroup{}
go func() {
for {
if in, ok := <-in; ok {
wg.Add(1)
go func() {
out <- mapper(in)
wg.Done()
}()
} else {
wg.Wait()
close(out)
break
}
}
}()
return out
}
// MapSync takes message and converts it into another type by map function.
// If input channel is closed then output channel is closed.
// Creates a new channel with the same capacity as input.
//
// # Strategies
//
// - Processing: Sync
// - Closing: Single
// - Capacity: Same
//
// # Usages
//
// // input := make(chan int, 4) with random values [1, 2, 3]
//
// output := MapSync(func(value int) string {
// fmt.Print(value)
// return fmt.Sprintf("val: %d", value)
// }, input)
//
// // stdout: 2 1 3
// // output: ["val: 1", "val: 2", "val: 3"]
func MapSync[Tin, Tout any](mapper func(Tin) Tout, in <-chan Tin) <-chan Tout {
out := make(chan Tout, cap(in))
queue := make(chan func() <-chan Tout, cap(in))
wg := sync.WaitGroup{}
go func() {
for {
if in, ok := <-in; ok {
wg.Add(1)
queue <- func() <-chan Tout {
out := make(chan Tout)
go func() {
out <- mapper(in)
close(out)
wg.Done()
}()
return out
}
} else {
wg.Wait()
close(queue)
break
}
}
}()
go func() {
for {
if handler, ok := <-queue; ok {
if data, ok := <-handler(); ok {
out <- data
}
} else {
close(out)
break
}
}
}()
return out
}
// MapSequential takes message and converts it into another type by map function.
// If input channel is closed then output channel is closed.
// Creates a new channel with the same capacity as input.
//
// # Strategies
//
// - Processing: Sequential
// - Closing: Single
// - Capacity: Same
//
// # Usages
//
// // input := make(chan int, 4) with random values [1, 2, 3]
//
// output := MapSequential(func(value int) string {
// fmt.Print(value)
// return fmt.Sprintf("val: %d", value)
// }, input)
//
// // stdout: 1 2 3
// // output: ["val: 1", "val: 2", "val: 3"]
func MapSequential[Tin, Tout any](mapper func(Tin) Tout, in <-chan Tin) <-chan Tout {
out := make(chan Tout, cap(in))
go func() {
for {
if in, ok := <-in; ok {
out <- mapper(in)
} else {
close(out)
break
}
}
}()
return out
}