Skip to content

Instantly share code, notes, and snippets.

@leonardo5621
Last active July 24, 2022 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardo5621/2384c5d0893b0aeb073d4101f9cb3132 to your computer and use it in GitHub Desktop.
Save leonardo5621/2384c5d0893b0aeb073d4101f9cb3132 to your computer and use it in GitHub Desktop.
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