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

feat: Add mapErrors to Result #884

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions stdlib/result.mc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ let _mergeErrors
= lam a. lam b.
{ warnings = mapUnion a.warnings b.warnings, errors = mapUnion a.errors b.errors }

-- Map the errors, if any, inside the `Result`. Preserves all warnings.
let _mapErrors
: all w. all e1. all e2. all a. (e1 -> e2) -> Result w e1 a -> Result w e2 a
= lam f. lam start.
switch start
case ResultOk r then ResultOk r
case ResultErr { warnings = warnings, errors = errors } then
let errors = map f (mapValues errors) in
didrikmunther marked this conversation as resolved.
Show resolved Hide resolved
let f = lam acc. lam err. mapInsert (gensym ()) err acc in
ResultErr {
warnings = warnings,
errors = foldl f (_emptyMap ()) errors
}
end

didrikmunther marked this conversation as resolved.
Show resolved Hide resolved
-- Update the value, if any, inside the `Result`. Preserves all errors
-- and warnings.
let _map
Expand Down Expand Up @@ -695,6 +710,7 @@ let result =
, map3 = _map3
, map4 = _map4
, map5 = _map5
, mapErrors = _mapErrors
, apply = _apply
, withAnnotations = _withAnnotations
-- Mapping, action can produce new errors and/or warnings
Expand Down
Loading