FilterResults implemented

This commit is contained in:
milarin 2023-03-25 11:45:55 +01:00
parent b602502992
commit a864cb930d

View File

@ -57,3 +57,34 @@ func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
return v 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
}