mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
Add List.walk_try!
This commit is contained in:
parent
ffa1dd5703
commit
ea2a007834
1 changed files with 15 additions and 1 deletions
|
@ -75,6 +75,7 @@ module [
|
|||
for_each!,
|
||||
for_each_try!,
|
||||
walk!,
|
||||
walk_try!,
|
||||
]
|
||||
|
||||
import Bool exposing [Bool, Eq]
|
||||
|
@ -1514,11 +1515,24 @@ for_each_try! = \list, func! ->
|
|||
walk! : List elem, state, (state, elem => state) => state
|
||||
walk! = \list, state, func! ->
|
||||
when list is
|
||||
[] ->
|
||||
[] ->
|
||||
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.
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue