ianmackenzie / ianmackenzie/elm-script

Collect-like function that doesn't give up on errors

Open
#19 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Elm
Stars
34
Forks
5
PR merge metrics
No merged PRs in 30d

Description

It would be useful to have a function that behaves like `collect` except that it doesn't stop when it encounters an error. Instead it always runs for every item and if there are any errors, it fails with a list of those errors, otherwise it succeeds with a list of values.

```elm
separate : List (Result e a) -> ( List e, List a )
separate =
List.foldl
(\result ( errors, oks ) ->
case result of
Ok ok ->
( errors, ok :: oks )

Err error ->
( error :: errors, oks )
)
( [], [] )

collectAll : (a -> Script x b) -> List a -> Script (List x) (List b)
collectAll getScript =
Script.collect (getScript >> Script.attempt)
>> Script.thenWith
(\results ->
case separate results of
( [], oks ) ->
Script.succeed oks

( errors, _ ) ->
Script.fail errors
)
```

One advantage of using this over `collect` is that you can provide better error messages. Rather than having the user having to fix each problem they encounter one at a time, they can be presented with a list of everything that went wrong.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.