I have a Result<'T, 'E> list
that I would like to turn into a single Result<'T list, 'E>
following these rules:
- If any
Result
is anError
then the result should be anError
- If the result is an
Error
it should be the firstError
in the list - If every result is an
OK
then the result should be anOk
and the list order should be maintained
So I had a go and implemented this as follows:
let all xs =
let folder = fun state next ->
match (state, next) with
| (Result.Ok ys, Result.Ok y) -> ys |> List.append [ y ] |> Result.Ok
| (Result.Error e, _) -> Result.Error e
| (_, Result.Error e) -> Result.Error e
Seq.fold folder (Result.Ok []) xs
However, this seems like something that might already have been implemented in the standard library. Has it?
Second, I have a computation expression for Result
like this:
type ResultBuilder () =
member this.Bind(x, f) =
match x with
| Result.Ok o -> f o
| Result.Error e -> Result.Error e
member this.Return(value) = Result.Ok value
member this.ReturnFrom(value) = value
let result = new ResultBuilder()
I can use all
inside of a result { ... }
but is further integration possible? For example by implementing ResultBuilder.For
?