mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 04:36:02 +03:00
new pipeline example from book
This commit is contained in:
parent
6dee362383
commit
d5aad34fce
62
concurrency/pipeline_from_book/main.go
Normal file
62
concurrency/pipeline_from_book/main.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func run() {
|
||||||
|
done := make(chan interface{})
|
||||||
|
defer close(done)
|
||||||
|
intStream := generator(done, 1, 2, 3, 4)
|
||||||
|
pipline := multiply(done, add(done, multiply(done, intStream, 2), 1), 2)
|
||||||
|
for v := range pipline {
|
||||||
|
spew.Dump(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func generator(done <-chan interface{}, integers ...int) <-chan int {
|
||||||
|
intStream := make(chan int)
|
||||||
|
go func() {
|
||||||
|
defer close(intStream)
|
||||||
|
for _, i := range integers {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
case intStream <- i:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return intStream
|
||||||
|
}
|
||||||
|
|
||||||
|
func multiply(done <-chan interface{}, intStream <-chan int, multiplier int) <-chan int {
|
||||||
|
multipliedStream := make(chan int)
|
||||||
|
go func() {
|
||||||
|
defer close(multipliedStream)
|
||||||
|
for i := range intStream {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
case multipliedStream <- i * multiplier:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return multipliedStream
|
||||||
|
}
|
||||||
|
|
||||||
|
func add(done <-chan interface{}, intStream <-chan int, additive int) <-chan int {
|
||||||
|
addStream := make(chan int)
|
||||||
|
go func() {
|
||||||
|
defer close(addStream)
|
||||||
|
for i := range intStream {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
case addStream <- i:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return addStream
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user