Last active
May 12, 2022 19:06
-
-
Save sshaplygin/ae7cd1d7e5adf061460155c6d11c260f to your computer and use it in GitHub Desktop.
hw06_pipeline_execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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