roc format builtins

This commit is contained in:
Richard Feldman 2022-07-06 17:36:03 -04:00
parent 496fd6e8bb
commit d2e5efadc0
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
3 changed files with 22 additions and 28 deletions

View file

@ -734,6 +734,7 @@ min = \list ->
when List.first list is
Ok initial ->
Ok (minHelp list initial)
Err ListWasEmpty ->
Err ListWasEmpty
@ -750,6 +751,7 @@ max = \list ->
when List.first list is
Ok initial ->
Ok (maxHelp list initial)
Err ListWasEmpty ->
Err ListWasEmpty
@ -782,6 +784,7 @@ find = \array, pred ->
when List.iterate array {} callback is
Continue {} ->
Err NotFound
Break found ->
Ok found
@ -863,6 +866,7 @@ iterHelp = \list, state, f, index, length ->
when f state (List.getUnsafe list index) is
Continue nextState ->
iterHelp list nextState f (index + 1) length
Break b ->
Break b
else

View file

@ -12,10 +12,8 @@ Result ok err : [Ok ok, Err err]
isOk : Result ok err -> Bool
isOk = \result ->
when result is
Ok _ ->
True
Err _ ->
False
Ok _ -> True
Err _ -> False
## Return True if the result indicates a failure, else return False
##
@ -23,10 +21,8 @@ isOk = \result ->
isErr : Result ok err -> Bool
isErr = \result ->
when result is
Ok _ ->
False
Err _ ->
True
Ok _ -> False
Err _ -> True
## If the result is `Ok`, return the value it holds. Otherwise, return
## the given default value.
@ -37,10 +33,8 @@ isErr = \result ->
withDefault : Result ok err, ok -> ok
withDefault = \result, default ->
when result is
Ok value ->
value
Err _ ->
default
Ok value -> value
Err _ -> default
## If the result is `Ok`, transform the value it holds by running a conversion
## function on it. Then return a new `Ok` holding the transformed value.
@ -56,10 +50,8 @@ withDefault = \result, default ->
map : Result a err, (a -> b) -> Result b err
map = \result, transform ->
when result is
Ok v ->
Ok (transform v)
Err e ->
Err e
Ok v -> Ok (transform v)
Err e -> Err e
## If the result is `Err`, transform the value it holds by running a conversion
## function on it. Then return a new `Err` holding the transformed value.
@ -72,10 +64,8 @@ map = \result, transform ->
mapErr : Result ok a, (a -> b) -> Result ok b
mapErr = \result, transform ->
when result is
Ok v ->
Ok v
Err e ->
Err (transform e)
Ok v -> Ok v
Err e -> Err (transform e)
## If the result is `Ok`, transform the entire result by running a conversion
## function on the value the `Ok` holds. Then return that new result.
@ -88,10 +78,8 @@ mapErr = \result, transform ->
after : Result a err, (a -> Result b err) -> Result b err
after = \result, transform ->
when result is
Ok v ->
transform v
Err e ->
Err e
Ok v -> transform v
Err e -> Err e
## If the result is `Err`, transform the entire result by running a conversion
## function on the value the `Err` holds. Then return that new result.
@ -104,7 +92,5 @@ after = \result, transform ->
afterErr : Result a err, (err -> Result a otherErr) -> Result a otherErr
afterErr = \result, transform ->
when result is
Ok v ->
Ok v
Err e ->
transform e
Ok v -> Ok v
Err e -> transform e

View file

@ -290,6 +290,7 @@ splitFirst = \haystack, needle ->
after = Str.substringUnsafe haystack (index + Str.countUtf8Bytes needle) remaining
Ok { before, after }
None ->
Err NotFound
@ -325,6 +326,7 @@ splitLast = \haystack, needle ->
after = Str.substringUnsafe haystack (index + Str.countUtf8Bytes needle) remaining
Ok { before, after }
None ->
Err NotFound
@ -344,6 +346,7 @@ lastMatchHelp = \haystack, needle, index ->
when Num.subChecked index 1 is
Ok nextIndex ->
lastMatchHelp haystack needle nextIndex
Err _ ->
None
@ -428,6 +431,7 @@ walkScalarsUntilHelp = \string, state, step, index, length ->
when step state scalar is
Continue newState ->
walkScalarsUntilHelp string newState step (index + bytesParsed) length
Break newState ->
newState
else