From a864cb930d4636ce6dce5e43dc5fd76d6ae29107 Mon Sep 17 00:00:00 2001 From: milarin Date: Sat, 25 Mar 2023 11:45:55 +0100 Subject: [PATCH] FilterResults implemented --- result.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/result.go b/result.go index 5fc9054..cfd7b7e 100644 --- a/result.go +++ b/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 +}