Skip to content

Add Result.Extra.collect #25

@gampleman

Description

@gampleman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    new functionRequest to add a new function to the library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions