From 110846d866e61110925702cb216020ffcefddf4c Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Tue, 5 Jul 2022 21:30:19 +0200 Subject: [PATCH] added Filter method --- filter.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 filter.go diff --git a/filter.go b/filter.go new file mode 100644 index 0000000..873f7c1 --- /dev/null +++ b/filter.go @@ -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 +}