-
-
Save leonardo5621/e0e347910b512e4f21458bb21dc382ea 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 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