mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +00:00
use unreachable
to implement walk
This commit is contained in:
parent
762258ae20
commit
8a611fdb34
1 changed files with 4 additions and 9 deletions
|
@ -373,17 +373,12 @@ contains = \list, needle ->
|
|||
## `fold`, `foldLeft`, or `foldl`.
|
||||
walk : List elem, state, (state, elem -> state) -> state
|
||||
walk = \list, state, func ->
|
||||
walkHelp list state func 0 (len list)
|
||||
walkHelp = \currentState, element -> Continue (func currentState element)
|
||||
|
||||
## internal helper
|
||||
walkHelp : List elem, state, (state, elem -> state), Nat, Nat -> state
|
||||
walkHelp = \list, state, f, index, length ->
|
||||
if index < length then
|
||||
nextState = f state (getUnsafe list index)
|
||||
when List.iterate list state walkHelp is
|
||||
Continue newState -> newState
|
||||
Break void -> List.unreachable void
|
||||
|
||||
walkHelp list nextState f (index + 1) length
|
||||
else
|
||||
state
|
||||
|
||||
## Note that in other languages, `walkBackwards` is sometimes called `reduceRight`,
|
||||
## `fold`, `foldRight`, or `foldr`.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue