added Filter method

This commit is contained in:
Timon Ringwald 2022-07-05 21:30:19 +02:00
parent 944ab39e28
commit 110846d866

16
filter.go Normal file
View File

@ -0,0 +1,16 @@
package channel
func Filter[T any](source <-chan T, filter func(T) bool) <-chan T {
out := make(chan T, cap(source))
go func() {
defer close(out)
for value := range source {
if filter(value) {
out <- value
}
}
}()
return out
}