initial commit
This commit is contained in:
38
find.go
Normal file
38
find.go
Normal file
@ -0,0 +1,38 @@
|
||||
package channel
|
||||
|
||||
import "context"
|
||||
|
||||
func FindFirst[T any](source <-chan T) *T {
|
||||
for v := range source {
|
||||
return &v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindFirstAndCancel[T any](source <-chan T, cancel context.CancelFunc) *T {
|
||||
defer cancel()
|
||||
for v := range 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
|
||||
}
|
||||
|
||||
func HasAny[T any](source <-chan T) bool {
|
||||
return FindFirst(source) != nil
|
||||
}
|
Reference in New Issue
Block a user