Compare commits

..

2 Commits

Author SHA1 Message Date
a864cb930d FilterResults implemented 2023-03-25 11:45:55 +01:00
b602502992 FilterSuccess implemented 2023-03-25 11:37:08 +01:00

View File

@ -48,3 +48,43 @@ func (r Result[T]) GetOrDefault(defaultValue T) T {
func (r Result[T]) Get() (T, error) {
return *r.value, r.err
}
func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
succeeded := Filter(source, func(r Result[T]) bool { return r.Success() })
return MapSuccessive(succeeded, func(r Result[T]) T {
v, _ := r.Get()
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
}