FilterResults implemented
This commit is contained in:
parent
b602502992
commit
a864cb930d
31
result.go
31
result.go
@ -57,3 +57,34 @@ func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
||||
return v
|
||||
})
|
||||
}
|
||||
|
||||
func FilterFail[T any](source <-chan Result[T]) <-chan T {
|
||||
failed := Filter(source, func(r Result[T]) bool { return r.Fail() })
|
||||
|
||||
return MapSuccessive(failed, func(r Result[T]) T {
|
||||
v, _ := r.Get()
|
||||
return v
|
||||
})
|
||||
}
|
||||
|
||||
func FilterResults[T any](source <-chan Result[T]) (succeeded <-chan T, failed <-chan error) {
|
||||
succ := make(chan T, cap(source))
|
||||
fail := make(chan error, cap(source))
|
||||
|
||||
go func() {
|
||||
defer close(succ)
|
||||
defer close(fail)
|
||||
|
||||
for r := range source {
|
||||
v, err := r.Get()
|
||||
|
||||
if err == nil {
|
||||
succ <- v
|
||||
} else {
|
||||
fail <- err
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return succ, fail
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user