Skip to content

Instantly share code, notes, and snippets.

@thevar1able
Created June 4, 2020 05:59
Show Gist options
  • Save thevar1able/4a160908907e8284111403d5cf75ab92 to your computer and use it in GitHub Desktop.
Save thevar1able/4a160908907e8284111403d5cf75ab92 to your computer and use it in GitHub Desktop.
package hw06_pipeline_execution //nolint:golint,stylecheck
type (
I = interface{}
In = <-chan I
Out = In
Bi = chan I
)
type Stage func(in In) (out Out)
func ExecutePipeline(in In, done In, stages ...Stage) Out {
prev := in
for _, fun := range stages {
prev = fun(prev)
next := make(Bi)
go func(done In, in In, next Bi) {
defer close(next)
for element := range in {
select {
case _, ok := <-done:
if !ok {
return
}
default:
next <- element
}
}
}(done, prev, next)
prev = next
}
return prev
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment