2022-12-19 18:01:38 +01:00
|
|
|
package channel
|
|
|
|
|
2023-01-16 15:13:07 +01:00
|
|
|
import "context"
|
|
|
|
|
2022-12-19 18:01:38 +01:00
|
|
|
func FindFirst[T any](source <-chan T) *T {
|
|
|
|
for v := range source {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 15:15:21 +01:00
|
|
|
func FindFirstAndCancel[T any](source <-chan T, cancel context.CancelFunc) *T {
|
2024-01-10 22:00:20 +01:00
|
|
|
defer cancel()
|
2023-01-16 15:13:07 +01:00
|
|
|
for v := range source {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-19 18:01:38 +01:00
|
|
|
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
|
|
|
|
}
|
2022-12-19 18:07:43 +01:00
|
|
|
|
|
|
|
func HasAny[T any](source <-chan T) bool {
|
|
|
|
return FindFirst(source) != nil
|
|
|
|
}
|