-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
operate3.go
46 lines (37 loc) · 816 Bytes
/
operate3.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
// Copyright (c) 2021-2024 Onur Cinar.
// The source code is provided under GNU AGPLv3 License.
// https://github.com/cinar/indicator
package helper
// Operate3 applies the provided operate function to corresponding values from three
// numeric input channels and sends the resulting values to an output channel.
//
// Example:
//
// add := helper.Operate3(ac, bc, cc, func(a, b, c int) int {
// return a + b + c
// })
func Operate3[A any, B any, C any, R any](ac <-chan A, bc <-chan B, cc <-chan C, o func(A, B, C) R) <-chan R {
rc := make(chan R)
go func() {
defer close(rc)
for {
an, ok := <-ac
if !ok {
break
}
bn, ok := <-bc
if !ok {
break
}
cn, ok := <-cc
if !ok {
break
}
rc <- o(an, bn, cn)
}
Drain(ac)
Drain(bc)
Drain(cc)
}()
return rc
}