Skip to content

Instantly share code, notes, and snippets.

@sshaplygin
Last active May 12, 2022 19:06
Show Gist options
  • Save sshaplygin/ae7cd1d7e5adf061460155c6d11c260f to your computer and use it in GitHub Desktop.
Save sshaplygin/ae7cd1d7e5adf061460155c6d11c260f to your computer and use it in GitHub Desktop.
hw06_pipeline_execution
package hw06pipelineexecution
type (
In = <-chan interface{}
Out = In
Bi = chan interface{}
)
type Stage func(in In) (out Out)
func doneStage(in In, done In) Out {
out := make(Bi)
go func() {
defer close(out)
for {
select {
case <-done:
return
case prev, ok := <-in:
if !ok {
return
}
out <- prev
}
}
}()
return out
}
func ExecutePipeline(in In, done In, stages ...Stage) Out {
pipe := in
for _, stage := range stages {
if stage != nil {
pipe = stage(doneStage(pipe, done))
}
}
return pipe
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment