Compare commits
2 Commits
984aae2553
...
a864cb930d
Author | SHA1 | Date | |
---|---|---|---|
a864cb930d | |||
b602502992 |
40
result.go
40
result.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user