mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-24 04:22:27 +00:00
Fix iterateBackwards
This commit is contained in:
parent
1c2047fbeb
commit
d60ad6b5af
1 changed files with 12 additions and 18 deletions
|
@ -795,14 +795,14 @@ findFirst = \list, pred ->
|
||||||
## Returns the last element of the list satisfying a predicate function.
|
## Returns the last element of the list satisfying a predicate function.
|
||||||
## If no satisfying element is found, an `Err NotFound` is returned.
|
## If no satisfying element is found, an `Err NotFound` is returned.
|
||||||
findLast : List elem, (elem -> Bool) -> Result elem [NotFound]*
|
findLast : List elem, (elem -> Bool) -> Result elem [NotFound]*
|
||||||
findLast = \array, pred ->
|
findLast = \list, pred ->
|
||||||
callback = \_, elem ->
|
callback = \_, elem ->
|
||||||
if pred elem then
|
if pred elem then
|
||||||
Break elem
|
Break elem
|
||||||
else
|
else
|
||||||
Continue {}
|
Continue {}
|
||||||
|
|
||||||
when iterateBackwards array {} callback is
|
when List.iterateBackwards list {} callback is
|
||||||
Continue {} -> Err NotFound
|
Continue {} -> Err NotFound
|
||||||
Break found -> Ok found
|
Break found -> Ok found
|
||||||
|
|
||||||
|
@ -825,12 +825,12 @@ findFirstIndex = \list, matcher ->
|
||||||
## satisfying a predicate function can be found.
|
## satisfying a predicate function can be found.
|
||||||
## If no satisfying element is found, an `Err NotFound` is returned.
|
## If no satisfying element is found, an `Err NotFound` is returned.
|
||||||
findLastIndex : List elem, (elem -> Bool) -> Result Nat [NotFound]*
|
findLastIndex : List elem, (elem -> Bool) -> Result Nat [NotFound]*
|
||||||
findLastIndex = \list, matcher ->
|
findLastIndex = \list, matches ->
|
||||||
foundIndex = iterateBackwards list (List.len list - 1) \index, elem ->
|
foundIndex = List.iterateBackwards list (List.len list) \prevIndex, elem ->
|
||||||
if matcher elem then
|
if matches elem then
|
||||||
Break index
|
Break (prevIndex - 1)
|
||||||
else
|
else
|
||||||
Continue (index - 1)
|
Continue (prevIndex - 1)
|
||||||
|
|
||||||
when foundIndex is
|
when foundIndex is
|
||||||
Break index -> Ok index
|
Break index -> Ok index
|
||||||
|
@ -981,11 +981,8 @@ iterHelp : List elem, s, (s, elem -> [Continue s, Break b]), Nat, Nat -> [Contin
|
||||||
iterHelp = \list, state, f, index, length ->
|
iterHelp = \list, state, f, index, length ->
|
||||||
if index < length then
|
if index < length then
|
||||||
when f state (List.getUnsafe list index) is
|
when f state (List.getUnsafe list index) is
|
||||||
Continue nextState ->
|
Continue nextState -> iterHelp list nextState f (index + 1) length
|
||||||
iterHelp list nextState f (index + 1) length
|
Break b -> Break b
|
||||||
|
|
||||||
Break b ->
|
|
||||||
Break b
|
|
||||||
else
|
else
|
||||||
Continue state
|
Continue state
|
||||||
|
|
||||||
|
@ -996,17 +993,14 @@ iterateBackwards = \list, init, func ->
|
||||||
iterBackwardsHelp list init func (List.len list)
|
iterBackwardsHelp list init func (List.len list)
|
||||||
|
|
||||||
## internal helper
|
## internal helper
|
||||||
iterBackwardsHelp : List elem, s, (s, elem -> [Continue s, Break b]), Nat, Nat -> [Continue s, Break b]
|
iterBackwardsHelp : List elem, s, (s, elem -> [Continue s, Break b]), Nat -> [Continue s, Break b]
|
||||||
iterBackwardsHelp = \list, state, f, prevIndex ->
|
iterBackwardsHelp = \list, state, f, prevIndex ->
|
||||||
if prevIndex > 0 then
|
if prevIndex > 0 then
|
||||||
index = prevIndex - 1
|
index = prevIndex - 1
|
||||||
|
|
||||||
when f state (List.getUnsafe list index) is
|
when f state (List.getUnsafe list index) is
|
||||||
Continue nextState ->
|
Continue nextState -> iterBackwardsHelp list nextState f index
|
||||||
iterHelp list nextState f index
|
Break b -> Break b
|
||||||
|
|
||||||
Break b ->
|
|
||||||
Break b
|
|
||||||
else
|
else
|
||||||
Continue state
|
Continue state
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue