resolve review comments

This commit is contained in:
Luke Boswell 2024-08-14 17:24:12 +10:00
parent 45f18bd64e
commit 6bae15c467
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
2 changed files with 42 additions and 5 deletions

View file

@ -81,11 +81,12 @@ mapErr = \result, transform ->
## Maps both the `Ok` and `Err` values of a `Result` to new values.
mapBoth : Result ok1 err1, (ok1 -> ok2), (err1 -> err2) -> Result ok2 err2
mapBoth = \result, okTransform, errTransform ->
result
|> Result.map okTransform
|> Result.mapErr errTransform
when result is
Ok val -> Ok (okTransform val)
Err err -> Err (errTransform err)
## Maps the `Ok` values of two `Result`s to a new value using a given transformation.
## Maps the `Ok` values of two `Result`s to a new value using a given transformation,
## or returns the first `Err` value encountered.
map2 : Result a err, Result b err, (a, b -> c) -> Result c err
map2 = \firstResult, secondResult, transform ->
when (firstResult, secondResult) is

View file

@ -357,7 +357,7 @@ fn roc_result_after_err() {
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn roc_result_map_other() {
fn roc_result_map_both() {
assert_evals_to!(
indoc!(
r#"
@ -383,7 +383,11 @@ fn roc_result_map_other() {
RocResult::err(RocStr::from("24")),
RocResult<RocStr, RocStr>
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
fn roc_result_map_two() {
assert_evals_to!(
indoc!(
r#"
@ -399,4 +403,36 @@ fn roc_result_map_other() {
RocResult::ok(14i64),
RocResult<i64, RocStr>
);
assert_evals_to!(
indoc!(
r#"
first : Result I64 Str
first = Err "foo"
second : Result I64 Str
second = Err "bar"
Result.map2 first second \a, b -> a + b
"#
),
RocResult::err(RocStr::from("foo")),
RocResult<i64, RocStr>
);
assert_evals_to!(
indoc!(
r#"
first : Result I64 Str
first = Ok 42
second : Result I64 Str
second = Err "bar"
Result.map2 first second \a, b -> a + b
"#
),
RocResult::err(RocStr::from("bar")),
RocResult<i64, RocStr>
);
}