Created
June 4, 2020 05:59
-
-
Save thevar1able/4a160908907e8284111403d5cf75ab92 to your computer and use it in GitHub Desktop.
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 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