Skip to content

Commit

Permalink
Merge pull request #7439 from imclerran/issue-7425
Browse files Browse the repository at this point in the history
Add List.walk! function
  • Loading branch information
lukewilliamboswell authored Jan 4, 2025
2 parents 07cd1c3 + acf8e33 commit 3d4dd5b
Show file tree
Hide file tree
Showing 46 changed files with 1,179 additions and 1,161 deletions.
18 changes: 18 additions & 0 deletions crates/compiler/builtins/roc/List.roc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module [
concatUtf8,
forEach!,
forEachTry!,
walk!,
]

import Bool exposing [Bool, Eq]
Expand Down Expand Up @@ -1466,3 +1467,20 @@ forEachTry! = \list, func! ->

Err err ->
Err err

## Build a value from the contents of a list, using an effectful function.
##
## ```roc
## now_multiples = List.walk! [1, 2, 3] [] \nums, i ->
## now = Utc.now! {} |> Utc.to_millis_since_epoch
## List.append nums (now * i)
## ```
##
## This is the same as [walk], except that the step function can have effects.
walk! : List elem, state, (state, elem => state) => state
walk! = \list, state, func! ->
when list is
[] -> state
[elem, .. as rest] ->
nextState = func! state elem
walk! rest nextState func!

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

54 changes: 27 additions & 27 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.

34 changes: 17 additions & 17 deletions crates/compiler/test_mono/generated/capture_void_layout_task.txt

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

Loading

0 comments on commit 3d4dd5b

Please sign in to comment.