github.com/songzhibin97/gkit@v1.2.13/concurrent/map_reduce.go (about) 1 package concurrent 2 3 func MapChan(in <-chan interface{}, fn func(interface{}) interface{}) <-chan interface{} { 4 out := make(chan interface{}, 1) 5 if in == nil { 6 close(out) 7 return out 8 } 9 go func() { 10 defer close(out) 11 for v := range in { 12 out <- fn(v) 13 } 14 }() 15 return out 16 } 17 18 func ReduceChan(in <-chan interface{}, fn func(r, v interface{}) interface{}) interface{} { 19 if in == nil { 20 return nil 21 } 22 out := <-in 23 for v := range in { 24 out = fn(out, v) 25 } 26 return out 27 }