Skip to content

Commit a69a326

Browse files
Merge pull request #7491 from imclerran/list-walk-try
Add List.walk_try!
2 parents 158691f + 541fb60 commit a69a326

File tree

46 files changed

+1421
-1387
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1421
-1387
lines changed

crates/compiler/builtins/roc/List.roc

+34-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module [
7575
for_each!,
7676
for_each_try!,
7777
walk!,
78+
walk_try!,
7879
]
7980

8081
import Bool exposing [Bool, Eq]
@@ -1514,7 +1515,39 @@ for_each_try! = \list, func! ->
15141515
walk! : List elem, state, (state, elem => state) => state
15151516
walk! = \list, state, func! ->
15161517
when list is
1517-
[] -> state
1518+
[] ->
1519+
state
1520+
15181521
[elem, .. as rest] ->
15191522
next_state = func!(state, elem)
15201523
walk!(rest, next_state, func!)
1524+
1525+
## Build a value from the contents of a list, using an effectful function that might fail.
1526+
##
1527+
## If the function returns `Err`, the iteration stops and the error is returned.
1528+
##
1529+
## ```
1530+
## names = try List.walk_try!(
1531+
## ["First", "Middle", "Last"],
1532+
## [],
1533+
## \accumulator, which ->
1534+
## try Stdout.write! ("$(which) name: ")
1535+
## name = try Stdin.line! ({})
1536+
## Ok (List.append accumulator name),
1537+
## )
1538+
## ```
1539+
##
1540+
## This is the same as [walk_try], except that the step function can have effects.
1541+
walk_try! : List elem, state, (state, elem => Result state err) => Result state err
1542+
walk_try! = \list, state, func! ->
1543+
when list is
1544+
[] ->
1545+
Ok(state)
1546+
1547+
[elem, .. as rest] ->
1548+
when func!(state, elem) is
1549+
Ok(next_state) ->
1550+
walk_try!(rest, next_state, func!)
1551+
1552+
Err(err) ->
1553+
Err(err)

crates/compiler/module/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,7 @@ define_builtins! {
15151515
92 LIST_WALK_FX: "walk!"
15161516
93 LIST_SPLIT_ON: "split_on"
15171517
94 LIST_SPLIT_ON_LIST: "split_on_list"
1518+
95 LIST_WALK_TRY_FX: "walk_try!"
15181519
}
15191520
7 RESULT: "Result" => {
15201521
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias

crates/compiler/test_mono/generated/anonymous_closure_in_polymorphic_expression_issue_4717.txt

+58-58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/compiler/test_mono/generated/call_function_in_empty_list.txt

+34-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/compiler/test_mono/generated/call_function_in_empty_list_unbound.txt

+34-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)