mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-20 07:17:50 +03:00
Simplify List.walk (e.g. for dev backend)
This commit is contained in:
parent
6c8b1963ae
commit
bac445b39b
@ -459,9 +459,18 @@ contains = \list, needle ->
|
||||
## Note that in other languages, `walk` is sometimes called `reduce`,
|
||||
## `fold`, `foldLeft`, or `foldl`.
|
||||
walk : List elem, state, (state, elem -> state) -> state
|
||||
walk = \list, state, func ->
|
||||
walkHelp : _, _ -> [Continue _, Break []]
|
||||
walkHelp = \currentState, element -> Continue (func currentState element)
|
||||
walk = \list, init, func ->
|
||||
walkHelp list init func 0 (List.len list)
|
||||
|
||||
## internal helper
|
||||
walkHelp : List elem, s, (s, elem -> s), Nat, Nat -> s
|
||||
walkHelp = \list, state, f, index, length ->
|
||||
if index < length then
|
||||
nextState = f state (List.getUnsafe list index)
|
||||
|
||||
walkHelp list nextState f (Num.addWrap index 1) length
|
||||
else
|
||||
state
|
||||
|
||||
when List.iterate list state walkHelp is
|
||||
Continue newState -> newState
|
||||
|
Loading…
Reference in New Issue
Block a user