elmcraft / elmcraft/core-extra

Add Result.Extra.collect

Open
#25 4 comments 0 reactions 0 assignees View on GitHub
new function
Dominant language
Elm
Stars
31
Forks
17
PR merge metrics
No merged PRs in 30d

Description

## `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.

```elm

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the Result.Extra module and its existing combine function, then compare the proposed collect signature and fold-based behavior with the surrounding API. Done means exposing collect with the requested Result (List e) (List a) behavior and covering the cases described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
elm
Domain
developer-experience
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.