more FindFirst variants

This commit is contained in:
milarin 2023-01-16 15:13:07 +01:00
parent ba2efffd8e
commit 3a80460c18

19
find.go
View File

@ -1,5 +1,7 @@
package channel package channel
import "context"
func FindFirst[T any](source <-chan T) *T { func FindFirst[T any](source <-chan T) *T {
for v := range source { for v := range source {
return &v return &v
@ -7,6 +9,14 @@ func FindFirst[T any](source <-chan T) *T {
return nil 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 { func FindFirstAndFlush[T any](source <-chan T) *T {
for v := range source { for v := range source {
go Flush(source) go Flush(source)
@ -15,6 +25,15 @@ func FindFirstAndFlush[T any](source <-chan T) *T {
return nil 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 { func FindLast[T any](source <-chan T) *T {
var last *T = new(T) var last *T = new(T)
found := false found := false