Inline inc and dec

This is for the dev backend's benefit
This commit is contained in:
Richard Feldman 2023-08-15 02:38:16 -04:00
parent fd866a5a64
commit df21104457
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
2 changed files with 21 additions and 29 deletions

View file

@ -391,7 +391,7 @@ repeat = \value, count ->
repeatHelp : a, Nat, List a -> List a
repeatHelp = \value, count, accum ->
if count > 0 then
repeatHelp value (dec count) (List.appendUnsafe accum value)
repeatHelp value (Num.subWrap count 1) (List.appendUnsafe accum value)
else
accum
@ -405,7 +405,7 @@ reverse = \list ->
reverseHelp = \list, left, right ->
if left < right then
reverseHelp (List.swap list left right) (inc left) (dec right)
reverseHelp (List.swap list left right) (Num.addWrap left 1) (Num.subWrap right 1)
else
list
@ -420,7 +420,7 @@ join = \lists ->
totalLength =
List.walk lists 0 (\state, list -> Num.addWrap state (List.len list))
List.walk lists (List.withCapacity totalLength) (\state, list -> List.concat state list)
List.walk lists (List.withCapacity totalLength) \state, list -> List.concat state list
contains : List a, a -> Bool where a implements Eq
contains = \list, needle ->
@ -478,7 +478,7 @@ walkBackwardsHelp = \list, state, f, indexPlusOne ->
if indexPlusOne == 0 then
state
else
index = dec indexPlusOne
index = Num.subWrap indexPlusOne 1
nextState = f state (getUnsafe list index)
walkBackwardsHelp list nextState f index
@ -590,9 +590,9 @@ keepIfHelp : List a, (a -> Bool), Nat, Nat, Nat -> List a
keepIfHelp = \list, predicate, kept, index, length ->
if index < length then
if predicate (List.getUnsafe list index) then
keepIfHelp (List.swap list kept index) predicate (inc kept) (inc index) length
keepIfHelp (List.swap list kept index) predicate (Num.addWrap kept 1) (Num.addWrap index 1) length
else
keepIfHelp list predicate kept (inc index) length
keepIfHelp list predicate kept (Num.addWrap index 1) length
else
List.takeFirst list kept
@ -619,7 +619,7 @@ countIf : List a, (a -> Bool) -> Nat
countIf = \list, predicate ->
walkState = \state, elem ->
if predicate elem then
inc state
Num.addWrap state 1
else
state
@ -712,7 +712,7 @@ mapWithIndexHelp = \src, dest, func, index, length ->
mappedElem = func elem index
newDest = List.appendUnsafe dest mappedElem
mapWithIndexHelp src newDest func (inc index) length
mapWithIndexHelp src newDest func (Num.addWrap index 1) length
else
dest
@ -817,7 +817,7 @@ rangeLengthHelp = \accum, i, remaining, calcNext ->
else
when i is
Ok val ->
rangeLengthHelp (List.appendUnsafe accum val) (calcNext val) (dec remaining) calcNext
rangeLengthHelp (List.appendUnsafe accum val) (calcNext val) (Num.subWrap remaining 1) calcNext
Err _ ->
# We went past the end of the numeric range and there is no next.
@ -1036,7 +1036,7 @@ findFirstIndex = \list, matcher ->
if matcher elem then
Break index
else
Continue (inc index)
Continue (Num.addWrap index 1)
when foundIndex is
Break index -> Ok index
@ -1048,7 +1048,7 @@ findFirstIndex = \list, matcher ->
findLastIndex : List elem, (elem -> Bool) -> Result Nat [NotFound]
findLastIndex = \list, matches ->
foundIndex = List.iterateBackwards list (List.len list) \prevIndex, elem ->
answer = dec prevIndex
answer = Num.subWrap prevIndex 1
if matches elem then
Break answer
@ -1153,7 +1153,7 @@ splitFirst = \list, delimiter ->
when List.findFirstIndex list (\elem -> elem == delimiter) is
Ok index ->
before = List.sublist list { start: 0, len: index }
after = List.sublist list { start: inc index, len: Num.subWrap (List.len list) index |> dec }
after = List.sublist list { start: Num.addWrap index 1, len: Num.subWrap (List.len list) index |> Num.subWrap 1 }
Ok { before, after }
@ -1169,7 +1169,7 @@ splitLast = \list, delimiter ->
when List.findLastIndex list (\elem -> elem == delimiter) is
Ok index ->
before = List.sublist list { start: 0, len: index }
after = List.sublist list { start: inc index, len: Num.subWrap (List.len list) index |> dec }
after = List.sublist list { start: Num.addWrap index 1, len: Num.subWrap (List.len list) index |> Num.subWrap 1 }
Ok { before, after }
@ -1204,7 +1204,7 @@ walkTryHelp : List elem, state, (state, elem -> Result state err), Nat, Nat -> R
walkTryHelp = \list, state, f, index, length ->
if index < length then
when f state (List.getUnsafe list index) is
Ok nextState -> walkTryHelp list nextState f (inc index) length
Ok nextState -> walkTryHelp list nextState f (Num.addWrap index 1) length
Err b -> Err b
else
Ok state
@ -1219,7 +1219,7 @@ iterHelp : List elem, s, (s, elem -> [Continue s, Break b]), Nat, Nat -> [Contin
iterHelp = \list, state, f, index, length ->
if index < length then
when f state (List.getUnsafe list index) is
Continue nextState -> iterHelp list nextState f (inc index) length
Continue nextState -> iterHelp list nextState f (Num.addWrap index 1) length
Break b -> Break b
else
Continue state
@ -1234,16 +1234,10 @@ iterateBackwards = \list, init, func ->
iterBackwardsHelp : List elem, s, (s, elem -> [Continue s, Break b]), Nat -> [Continue s, Break b]
iterBackwardsHelp = \list, state, f, prevIndex ->
if prevIndex > 0 then
index = dec prevIndex
index = Num.subWrap prevIndex 1
when f state (List.getUnsafe list index) is
Continue nextState -> iterBackwardsHelp list nextState f index
Break b -> Break b
else
Continue state
inc : Int a -> Int a
inc = \num -> Num.addWrap num 1
dec : Int a -> Int a
dec = \num -> Num.subWrap num 1

View file

@ -757,7 +757,7 @@ firstMatchHelp = \haystack, needle, index, lastPossible ->
if matchesAt haystack index needle then
Some index
else
firstMatchHelp haystack needle (inc index) lastPossible
firstMatchHelp haystack needle (Num.addWrap index 1) lastPossible
else
None
@ -847,8 +847,8 @@ matchesAtHelp = \state ->
doesRestMatch =
matchesAtHelp
{ state &
haystackIndex: inc haystackIndex,
needleIndex: inc needleIndex,
haystackIndex: Num.addWrap haystackIndex 1,
needleIndex: Num.addWrap needleIndex 1,
}
doesThisMatch && doesRestMatch
@ -871,7 +871,7 @@ walkUtf8WithIndexHelp = \string, state, step, index, length ->
byte = Str.getUnsafe string index
newState = step state byte index
walkUtf8WithIndexHelp string newState step (inc index) length
walkUtf8WithIndexHelp string newState step (Num.addWrap index 1) length
else
state
@ -892,7 +892,7 @@ walkUtf8Help = \str, state, step, index, length ->
byte = Str.getUnsafe str index
newState = step state byte
walkUtf8Help str newState step (inc index) length
walkUtf8Help str newState step (Num.addWrap index 1) length
else
state
@ -995,5 +995,3 @@ strToNumHelp = \string ->
## ```
withPrefix : Str, Str -> Str
withPrefix = \str, prefix -> Str.concat prefix str
inc = \num -> Num.addWrap num 1