Open gampleman opened 9 months ago
collect : List (Result e a) -> Result (List e) (List a)
Particularly useful for validation, it collects all the errors in the list, so they can all be shown at once to the user.
Validation almost always needs this, but pretty much any library where we want to show good error messages needs to not loose information.
This tends to be in practice more useful than Result.Extra.combine, as when using Result, we actually care about the error type.
Result.Extra.combine
Result
collect : List (Result e a) -> Result (List e) (List a) collect = List.foldr (\ra soFar -> case (ra, soFar) of (Err x, Err y) -> Err (x :: y) (Ok _, Err _) -> soFar (Err x, Ok _) -> Err [ x ] (Ok x, Ok y) -> Ok (x :: r) ) (Ok [])
or some such. Not terrible to write, but not exactly a handy one-liner either.
collect is just a spitball idea, moderate bike-shedding welcome.
collect
classify? I don't love it though
classify
Also, if you do this you should have it be List (Result e a) -> Result (e, List e) (List a)
List (Result e a) -> Result (e, List e) (List a)
collect : List (Result e a) -> Result (List e) (List a)
Particularly useful for validation, it collects all the errors in the list, so they can all be shown at once to the user.
Motivating use case
Validation almost always needs this, but pretty much any library where we want to show good error messages needs to not loose information.
Rationale
This tends to be in practice more useful than
Result.Extra.combine
, as when usingResult
, we actually care about the error type.or some such. Not terrible to write, but not exactly a handy one-liner either.
Name
collect
is just a spitball idea, moderate bike-shedding welcome.