NLinker

Merge 2 channels function

Jun 11th, 2020 (edited)
1,689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.62 KB | None | 0 0
  1. package main
  2.  
  3. func main() {}
  4.  
  5. func Merge2Channels(f func(int) int, in1 <-chan int, in2 <-chan int, out chan<- int, n int) {
  6.   go func() {
  7.     ww1 := make(chan chan int, n)
  8.     ww2 := make(chan chan int, n)
  9.  
  10.     go func() {
  11.       for i := 0; i < n; i++ {
  12.         f1 := <-<-ww1
  13.         f2 := <-<-ww2
  14.         out <- f1 + f2
  15.       }
  16.     }()
  17.  
  18.     input := func(in <-chan int, ww chan chan int) {
  19.       for i := 0; i < n; i++ {
  20.         w := make(chan int)
  21.         ww <- w
  22.         x := <-in
  23.         go func(w chan int, x int) { w <- f(x) }(w, x)
  24.       }
  25.     }
  26.  
  27.     go input(in1, ww1)
  28.     go input(in2, ww2)
  29.   }()
  30. }
Add Comment
Please, Sign In to add comment