Skip to content

Instantly share code, notes, and snippets.

@leonardo5621
Last active July 24, 2022 00:08
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/e0e347910b512e4f21458bb21dc382ea to your computer and use it in GitHub Desktop.
Save leonardo5621/e0e347910b512e4f21458bb21dc382ea to your computer and use it in GitHub Desktop.
func Filter(done <-chan struct{}, inputStream <-chan int, operator func(int)bool) <-chan int {
filteredStream := make(chan int)
go func() {
defer close(filteredStream)
for {
select {
case <-done:
return
case i, ok := <-inputStream:
if !ok {
return
}
if !operator(i) { break }
select {
case <-done:
return
case filteredStream <- i:
}
}
}
}()
return filteredStream
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment