17 lines
250 B
Go
17 lines
250 B
Go
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
|
|
}
|