Skip to content

Commit

Permalink
Merge pull request #7491 from imclerran/list-walk-try
Browse files Browse the repository at this point in the history
Add List.walk_try!
  • Loading branch information
lukewilliamboswell authored Jan 9, 2025
2 parents 158691f + 541fb60 commit a69a326
Show file tree
Hide file tree
Showing 46 changed files with 1,421 additions and 1,387 deletions.
35 changes: 34 additions & 1 deletion crates/compiler/builtins/roc/List.roc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module [
for_each!,
for_each_try!,
walk!,
walk_try!,
]

import Bool exposing [Bool, Eq]
Expand Down Expand Up @@ -1514,7 +1515,39 @@ for_each_try! = \list, func! ->
walk! : List elem, state, (state, elem => state) => state
walk! = \list, state, func! ->
when list is
[] -> state
[] ->
state

[elem, .. as rest] ->
next_state = func!(state, elem)
walk!(rest, next_state, func!)

## Build a value from the contents of a list, using an effectful function that might fail.
##
## If the function returns `Err`, the iteration stops and the error is returned.
##
## ```
## names = try List.walk_try!(
## ["First", "Middle", "Last"],
## [],
## \accumulator, which ->
## try Stdout.write! ("$(which) name: ")
## name = try Stdin.line! ({})
## Ok (List.append accumulator name),
## )
## ```
##
## This is the same as [walk_try], except that the step function can have effects.
walk_try! : List elem, state, (state, elem => Result state err) => Result state err
walk_try! = \list, state, func! ->
when list is
[] ->
Ok(state)

[elem, .. as rest] ->
when func!(state, elem) is
Ok(next_state) ->
walk_try!(rest, next_state, func!)

Err(err) ->
Err(err)
1 change: 1 addition & 0 deletions crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,7 @@ define_builtins! {
92 LIST_WALK_FX: "walk!"
93 LIST_SPLIT_ON: "split_on"
94 LIST_SPLIT_ON_LIST: "split_on_list"
95 LIST_WALK_TRY_FX: "walk_try!"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias
Expand Down

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

68 changes: 34 additions & 34 deletions crates/compiler/test_mono/generated/call_function_in_empty_list.txt

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

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

Loading

0 comments on commit a69a326

Please sign in to comment.