-
-
Save leonardo5621/2384c5d0893b0aeb073d4101f9cb3132 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
func Map(done <-chan struct{}, inputStream <-chan int, operator func(int)int) <-chan int { | |
// 1 | |
mappedStream := make(chan int) | |
go func() { | |
defer close(mappedStream) | |
// 2 | |
for { | |
select { | |
case <-done: | |
return | |
// 3 | |
case i, ok := <-inputStream: | |
if !ok { return } | |
//4 | |
select { | |
case <-done: | |
return | |
case mappedStream <- operator(i): | |
} | |
} | |
} | |
}() | |
return mappedStream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment