From a85ba0011d89ce05bae6cc8ca6abf28577a8133c Mon Sep 17 00:00:00 2001 From: milarin Date: Mon, 19 Dec 2022 18:01:38 +0100 Subject: [PATCH] Find operations added --- find.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 find.go diff --git a/find.go b/find.go new file mode 100644 index 0000000..8cbd3d8 --- /dev/null +++ b/find.go @@ -0,0 +1,32 @@ +package channel + +func FindFirst[T any](source <-chan T) *T { + for v := range source { + return &v + } + return nil +} + +func FindFirstAndFlush[T any](source <-chan T) *T { + for v := range source { + Flush(source) + return &v + } + return nil +} + +func FindLast[T any](source <-chan T) *T { + var last *T = new(T) + found := false + + for v := range source { + *last = v + found = true + } + + if !found { + return nil + } + + return last +}