From 3a80460c18eccd48468d1ab0972cd578c8fc87ec Mon Sep 17 00:00:00 2001 From: milarin Date: Mon, 16 Jan 2023 15:13:07 +0100 Subject: [PATCH] more FindFirst variants --- find.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/find.go b/find.go index b5c3f06..9a2fa6f 100644 --- a/find.go +++ b/find.go @@ -1,5 +1,7 @@ package channel +import "context" + func FindFirst[T any](source <-chan T) *T { for v := range source { return &v @@ -7,6 +9,14 @@ func FindFirst[T any](source <-chan T) *T { return nil } +func FindFirstAndCancel[T any](cancel context.CancelFunc, source <-chan T) *T { + for v := range source { + cancel() + return &v + } + return nil +} + func FindFirstAndFlush[T any](source <-chan T) *T { for v := range source { go Flush(source) @@ -15,6 +25,15 @@ func FindFirstAndFlush[T any](source <-chan T) *T { return nil } +func FindFirstAndCancelFlush[T any](cancel context.CancelFunc, source <-chan T) *T { + for v := range source { + cancel() + go Flush(source) + return &v + } + return nil +} + func FindLast[T any](source <-chan T) *T { var last *T = new(T) found := false