Skip to content

Commit

Permalink
Merge pull request #7521 from imclerran/result-map-ok
Browse files Browse the repository at this point in the history
Replace `Result.map` with `Result.map_ok`
  • Loading branch information
lukewilliamboswell authored Jan 16, 2025
2 parents b84f0d1 + 14d99c6 commit 9d37c90
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion crates/cli/tests/benchmarks/AStar.roc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cheapest_open = \cost_fn, model ->
)
|> Quicksort.sort_by(.cost)
|> List.first
|> Result.map(.position)
|> Result.map_ok(.position)
|> Result.map_err(\_ -> {})

reconstruct_path : Dict position position, position -> List position where position implements Hash & Eq
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/tests/benchmarks/closure.roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ main! = \{} ->
closure1 : {} -> Result {} []
closure1 = \_ ->
Ok(foo(to_unit_borrowed, "a long string such that it's malloced"))
|> Result.map(\_ -> {})
|> Result.map_ok(\_ -> {})

to_unit_borrowed = \x -> Str.count_utf8_bytes(x)

Expand All @@ -25,8 +25,8 @@ closure2 = \_ ->
x = "a long string such that it's malloced"

Ok({})
|> Result.map(\_ -> x)
|> Result.map(to_unit)
|> Result.map_ok(\_ -> x)
|> Result.map_ok(to_unit)

to_unit = \_ -> {}

Expand All @@ -37,7 +37,7 @@ closure3 = \_ ->
x = "a long string such that it's malloced"

Ok({})
|> Result.try(\_ -> Ok(x) |> Result.map(\_ -> {}))
|> Result.try(\_ -> Ok(x) |> Result.map_ok(\_ -> {}))

# # ---
closure4 : {} -> Result {} []
Expand All @@ -47,4 +47,4 @@ closure4 = \_ ->

Ok({})
|> Result.try(\_ -> Ok(x))
|> Result.map(\_ -> {})
|> Result.map_ok(\_ -> {})
2 changes: 1 addition & 1 deletion crates/compiler/builtins/roc/Decode.roc
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ from_bytes = \bytes, fmt ->

## Transform the `val` of a [DecodeResult]
map_result : DecodeResult a, (a -> b) -> DecodeResult b
map_result = \{ result, rest }, mapper -> { result: Result.map(result, mapper), rest }
map_result = \{ result, rest }, mapper -> { result: Result.map_ok(result, mapper), rest }
2 changes: 1 addition & 1 deletion crates/compiler/builtins/roc/List.roc
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ map_try = \list, to_result ->
list,
[],
\state, elem ->
Result.map(
Result.map_ok(
to_result(elem),
\ok ->
List.append(state, ok),
Expand Down
10 changes: 5 additions & 5 deletions crates/compiler/builtins/roc/Result.roc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module [
Result,
is_ok,
is_err,
map,
map_ok,
map_err,
map_both,
map2,
Expand Down Expand Up @@ -54,14 +54,14 @@ with_default = \result, default ->
## function on it. Then returns a new `Ok` holding the transformed value. If the
## result is `Err`, this has no effect. Use [map_err] to transform an `Err`.
## ```roc
## Result.map(Ok(12), Num.neg)
## Result.map(Err("yipes!"), Num.neg)
## Result.map_ok(Ok(12), Num.neg)
## Result.map_ok(Err("yipes!"), Num.neg)
## ```
##
## Functions like `map` are common in Roc; see for example [List.map],
## `Set.map`, and `Dict.map`.
map : Result a err, (a -> b) -> Result b err
map = \result, transform ->
map_ok : Result a err, (a -> b) -> Result b err
map_ok = \result, transform ->
when result is
Ok(v) -> Ok(transform(v))
Err(e) -> Err(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cheapest_open = \cost_function, model ->
Ok(smallest_so_far)

Set.walk(model.open_set, Err(KeyNotFound), folder)
|> Result.map(\x -> x.position)
|> Result.map_ok(\x -> x.position)

reconstruct_path : Map position position, position -> List position
reconstruct_path = \came_from, goal ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cheapest_open = \cost_function, model ->
Ok(smallest_so_far)

Set.walk(model.open_set, Err(KeyNotFound), folder)
|> Result.map(\x -> x.position)
|> Result.map_ok(\x -> x.position)

reconstruct_path : Dict position position, position -> List position where position implements Hash & Eq
reconstruct_path = \came_from, goal ->
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ define_builtins! {
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias
1 RESULT_IS_ERR: "is_err"
2 RESULT_ON_ERR: "on_err"
3 RESULT_MAP: "map"
3 RESULT_MAP_OK: "map_ok"
4 RESULT_MAP_ERR: "map_err"
5 RESULT_WITH_DEFAULT: "with_default"
6 RESULT_TRY: "try"
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/solve/tests/solve_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3479,7 +3479,7 @@ mod solve_expr {
Ok { position, cost: 0.0 }
Set.walk model.open_set (Ok { position: boom {}, cost: 0.0 }) folder
|> Result.map (\x -> x.position)
|> Result.map_ok (\x -> x.position)
astar : Model position -> Result position [KeyNotFound] where position implements Hash & Eq
astar = \model -> cheapest_open model
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/test_gen/src/gen_abilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ fn decode() {
# impl MDecoding for MyU8
decoder = @MDecoder \lst, fmt ->
{ result, rest } = decode_with lst u8 fmt
{ result: Result.map result (\n -> @MyU8 n), rest }
{ result: Result.map_ok result (\n -> @MyU8 n), rest }
myU8 =
when from_bytes [15] (@Linear {}) is
Expand Down Expand Up @@ -2210,7 +2210,7 @@ fn issue_4772_weakened_monomorphic_destructure() {
Err (ParsingFailure "not a number")
main =
get_number |> Result.map .val |> Result.with_default 0
get_number |> Result.map_ok .val |> Result.with_default 0
"#
),
1234i64,
Expand Down
18 changes: 9 additions & 9 deletions crates/compiler/test_gen/src/gen_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,15 +554,15 @@ fn list_split_first() {
assert_evals_to!(
r"
List.split_first [2, 3, 0, 4, 0, 6, 0, 8, 9] 0
|> Result.map .before
|> Result.map_ok .before
",
RocResult::ok(RocList::<i64>::from_slice(&[2, 3])),
RocResult<RocList<i64>, ()>
);
assert_evals_to!(
r"
List.split_first [2, 3, 0, 4, 0, 6, 0, 8, 9] 0
|> Result.map .after
|> Result.map_ok .after
",
RocResult::ok(RocList::<i64>::from_slice(&[4, 0, 6, 0, 8, 9])),
RocResult<RocList<i64>, ()>
Expand All @@ -587,15 +587,15 @@ fn list_split_last() {
assert_evals_to!(
r"
List.split_last [2, 3, 0, 4, 0, 6, 0, 8, 9] 0
|> Result.map .before
|> Result.map_ok .before
",
RocResult::ok(RocList::<i64>::from_slice(&[2, 3, 0, 4, 0, 6])),
RocResult<RocList<i64>, ()>
);
assert_evals_to!(
r"
List.split_last [2, 3, 0, 4, 0, 6, 0, 8, 9] 0
|> Result.map .after
|> Result.map_ok .after
",
RocResult::ok(RocList::<i64>::from_slice(&[8, 9])),
RocResult<RocList<i64>, ()>
Expand Down Expand Up @@ -2164,7 +2164,7 @@ fn first_wildcard_empty_list() {
assert_evals_to!(
indoc!(
r"
List.last [] |> Result.map (\_ -> 0i64)
List.last [] |> Result.map_ok (\_ -> 0i64)
"
),
RocResult::err(()),
Expand Down Expand Up @@ -2209,7 +2209,7 @@ fn last_wildcard_empty_list() {
assert_evals_to!(
indoc!(
r"
List.last [] |> Result.map (\_ -> 0i64)
List.last [] |> Result.map_ok (\_ -> 0i64)
"
),
RocResult::err(()),
Expand Down Expand Up @@ -2261,7 +2261,7 @@ fn get_wildcard_empty_list() {
indoc!(
r"
List.get [] 0
|> Result.map (\_ -> {})
|> Result.map_ok (\_ -> {})
"
),
RocResult::err(()),
Expand Down Expand Up @@ -2978,7 +2978,7 @@ fn list_min() {
indoc!(
r"
List.min []
|> Result.map (\_ -> {})
|> Result.map_ok (\_ -> {})
"
),
RocResult::err(()),
Expand All @@ -3003,7 +3003,7 @@ fn list_max() {
indoc!(
r"
List.max []
|> Result.map (\_ -> {})
|> Result.map_ok (\_ -> {})
"
),
RocResult::err(()),
Expand Down
10 changes: 5 additions & 5 deletions crates/compiler/test_gen/src/gen_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn result_map() {
result = Ok 2
result
|> Result.map (\x -> x + 1)
|> Result.map_ok (\x -> x + 1)
|> Result.with_default 0
"
),
Expand All @@ -71,7 +71,7 @@ fn result_map() {
result = Err {}
result
|> Result.map (\x -> x + 1)
|> Result.map_ok (\x -> x + 1)
|> Result.with_default 0
"
),
Expand Down Expand Up @@ -120,7 +120,7 @@ fn err_type_var() {
assert_evals_to!(
indoc!(
r"
Result.map (Ok 3) (\x -> x + 1)
Result.map_ok (Ok 3) (\x -> x + 1)
|> Result.with_default -1
"
),
Expand All @@ -138,7 +138,7 @@ fn err_type_var_annotation() {
ok : Result I64 *
ok = Ok 3
Result.map ok (\x -> x + 1)
Result.map_ok ok (\x -> x + 1)
|> Result.with_default -1
"
),
Expand All @@ -156,7 +156,7 @@ fn err_empty_tag_union() {
ok : Result I64 []
ok = Ok 3
Result.map ok (\x -> x + 1)
Result.map_ok ok (\x -> x + 1)
|> Result.with_default -1
"
),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/compiler/uitest/tests/ability/smoke/decoder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ decoder = @MDecoder \lst, fmt ->
#^^^^^^^{-1} MyU8#decoder(12): MDecoder MyU8 fmt where fmt implements MDecoderFormatting
when decode_with lst u8 fmt is
{ result, rest } ->
{ result: Result.map result (\n -> @MyU8 n), rest }
{ result: Result.map_ok result (\n -> @MyU8 n), rest }

my_u8 : Result MyU8 _
my_u8 = from_bytes [15] (@Linear {})
Expand Down

0 comments on commit 9d37c90

Please sign in to comment.