Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Result.Extra.collect #25

Open
gampleman opened this issue Sep 14, 2023 · 2 comments
Open

Add Result.Extra.collect #25

gampleman opened this issue Sep 14, 2023 · 2 comments
Labels
new function Request to add a new function to the library

Comments

@gampleman
Copy link
Collaborator

gampleman commented Sep 14, 2023

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 using Result, we actually care about the error type.

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.

Name

collect is just a spitball idea, moderate bike-shedding welcome.

@gampleman gampleman added the new function Request to add a new function to the library label Sep 14, 2023
@miniBill
Copy link
Collaborator

classify? I don't love it though

@miniBill
Copy link
Collaborator

miniBill commented Mar 6, 2024

Also, if you do this you should have it be List (Result e a) -> Result (e, List e) (List a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new function Request to add a new function to the library
Projects
None yet
Development

No branches or pull requests

2 participants