-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
duplicate.go
40 lines (34 loc) · 1.06 KB
/
duplicate.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
// Copyright (c) 2021-2024 Onur Cinar.
// The source code is provided under GNU AGPLv3 License.
// https://github.com/cinar/indicator
package helper
// Duplicate duplicates a given receive-only channel by reading each value coming out of
// that channel and sending them on requested number of new output channels.
//
// Example:
//
// expected := helper.SliceToChan([]float64{-10, 20, -4, -5})
// outputs := helper.Duplicates[float64](helper.SliceToChan(expected), 2)
//
// fmt.Println(helper.ChanToSlice(outputs[0])) // [-10, 20, -4, -5]
// fmt.Println(helper.ChanToSlice(outputs[1])) // [-10, 20, -4, -5]
func Duplicate[T any](input <-chan T, count int) []<-chan T {
// TODO(cinar): Find a way to cast as a directional channel array.
outputs := make([]chan T, count)
result := make([]<-chan T, count)
for i := range outputs {
outputs[i] = make(chan T, cap(input))
result[i] = outputs[i]
}
go func() {
for _, output := range outputs {
defer close(output)
}
for n := range input {
for _, output := range outputs {
output <- n
}
}
}()
return result
}