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

Add List.walk_try! #7491

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading