Rename Result.after to Result.try

This commit is contained in:
Richard Feldman 2022-07-17 17:25:12 -04:00
parent 31f7d72ce0
commit 0acab0eef3
No known key found for this signature in database
GPG Key ID: 7E4127D1E4241798
5 changed files with 39 additions and 39 deletions

View File

@ -1,5 +1,5 @@
interface Result
exposes [Result, isOk, isErr, map, mapErr, after, afterErr, withDefault]
exposes [Result, isOk, isErr, map, mapErr, try, afterErr, withDefault]
imports [Bool.{ Bool }]
## The result of an operation that could fail: either the operation went
@ -72,11 +72,11 @@ mapErr = \result, transform ->
##
## (If the result is `Err`, this has no effect. Use `afterErr` to transform an `Err`.)
##
## >>> Result.after (Ok -1) \num -> if num < 0 then Err "negative!" else Ok -num
## >>> Result.try (Ok -1) \num -> if num < 0 then Err "negative!" else Ok -num
##
## >>> Result.after (Err "yipes!") \num -> if num < 0 then Err "negative!" else Ok -num
after : Result a err, (a -> Result b err) -> Result b err
after = \result, transform ->
## >>> Result.try (Err "yipes!") \num -> if num < 0 then Err "negative!" else Ok -num
try : Result a err, (a -> Result b err) -> Result b err
try = \result, transform ->
when result is
Ok v -> transform v
Err e -> Err e
@ -84,7 +84,7 @@ after = \result, transform ->
## 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.
##
## (If the result is `Ok`, this has no effect. Use `after` to transform an `Ok`.)
## (If the result is `Ok`, this has no effect. Use `try` to transform an `Ok`.)
##
## >>> Result.afterErr (Ok 10) \errorNum -> Str.toNat errorNum
##

View File

@ -291,7 +291,7 @@ fn decode() {
fromBytes = \lst, fmt ->
when decodeWith lst decoder fmt is
{ result, rest } ->
Result.after result \val ->
Result.try result \val ->
if List.isEmpty rest
then Ok val
else Err (Leftover rest)

View File

@ -2479,9 +2479,9 @@ fn backpassing_result() {
main : I64
main =
helper =
x <- Result.after a
y <- Result.after (f x)
z <- Result.after (g y)
x <- Result.try a
y <- Result.try (f x)
z <- Result.try (g y)
Ok z

View File

@ -277,7 +277,7 @@ fn roc_result_after_on_ok() {
input : Result I64 Str
input = Ok 1
Result.after input \num ->
Result.try input \num ->
if num < 0 then Err "negative!" else Ok -num
"#),
RocResult::ok(-1),
@ -293,7 +293,7 @@ fn roc_result_after_on_err() {
input : Result I64 Str
input = (Err "already a string")
Result.after input \num ->
Result.try input \num ->
if num < 0 then Err "negative!" else Ok -num
"#),
RocResult::err(RocStr::from("already a string")),
@ -308,7 +308,7 @@ fn roc_result_after_err() {
r#"
result : Result Str I64
result =
Result.afterErr (Ok "already a string") \num ->
Result.tryErr (Ok "already a string") \num ->
if num < 0 then Ok "negative!" else Err -num
result
@ -321,7 +321,7 @@ fn roc_result_after_err() {
r#"
result : Result Str I64
result =
Result.afterErr (Err 100) \num ->
Result.tryErr (Err 100) \num ->
if num < 0 then Ok "negative!" else Err -num
result

View File

@ -239,13 +239,13 @@ interpretCtxLoop = \ctx ->
when result is
Ok (T 0xB8 newCtx) ->
result2 =
(T popCtx index) <- Result.after (popNumber newCtx)
(T popCtx index) <- Result.try (popNumber newCtx)
# I think Num.abs is too restrictive, it should be able to produce a natural number, but it seem to be restricted to signed numbers.
size = List.len popCtx.stack - 1
offset = Num.intCast size - index
if offset >= 0 then
stackVal <- Result.after (List.get popCtx.stack (Num.intCast offset))
stackVal <- Result.try (List.get popCtx.stack (Num.intCast offset))
Ok (Context.pushStack popCtx stackVal)
else
Err OutOfBounds
@ -289,7 +289,7 @@ stepExecCtx = \ctx, char ->
# `!` execute lambda
Task.fromResult
(
(T popCtx bytes) <- Result.after (popLambda ctx)
(T popCtx bytes) <- Result.try (popLambda ctx)
Ok { popCtx & scopes: List.append popCtx.scopes { data: None, buf: bytes, index: 0, whileInfo: None } }
)
@ -297,8 +297,8 @@ stepExecCtx = \ctx, char ->
# `?` if
Task.fromResult
(
(T popCtx1 bytes) <- Result.after (popLambda ctx)
(T popCtx2 n1) <- Result.after (popNumber popCtx1)
(T popCtx1 bytes) <- Result.try (popLambda ctx)
(T popCtx2 n1) <- Result.try (popNumber popCtx1)
if n1 == 0 then
Ok popCtx2
else
@ -309,8 +309,8 @@ stepExecCtx = \ctx, char ->
# `#` while
Task.fromResult
(
(T popCtx1 body) <- Result.after (popLambda ctx)
(T popCtx2 cond) <- Result.after (popLambda popCtx1)
(T popCtx1 body) <- Result.try (popLambda ctx)
(T popCtx2 cond) <- Result.try (popLambda popCtx1)
last = (List.len popCtx2.scopes - 1)
when List.get popCtx2.scopes last is
@ -343,8 +343,8 @@ stepExecCtx = \ctx, char ->
0x5C ->
# `\` swap
result2 =
(T popCtx1 n1) <- Result.after (Context.popStack ctx)
(T popCtx2 n2) <- Result.after (Context.popStack popCtx1)
(T popCtx1 n1) <- Result.try (Context.popStack ctx)
(T popCtx2 n2) <- Result.try (Context.popStack popCtx1)
Ok (Context.pushStack (Context.pushStack popCtx2 n1) n2)
when result2 is
@ -358,9 +358,9 @@ stepExecCtx = \ctx, char ->
0x40 ->
# `@` rot
result2 =
(T popCtx1 n1) <- Result.after (Context.popStack ctx)
(T popCtx2 n2) <- Result.after (Context.popStack popCtx1)
(T popCtx3 n3) <- Result.after (Context.popStack popCtx2)
(T popCtx1 n1) <- Result.try (Context.popStack ctx)
(T popCtx2 n2) <- Result.try (Context.popStack popCtx1)
(T popCtx3 n3) <- Result.try (Context.popStack popCtx2)
Ok (Context.pushStack (Context.pushStack (Context.pushStack popCtx3 n2) n1) n3)
when result2 is
@ -381,13 +381,13 @@ stepExecCtx = \ctx, char ->
# `O` also treat this as pick for easier script writing
Task.fromResult
(
(T popCtx index) <- Result.after (popNumber ctx)
(T popCtx index) <- Result.try (popNumber ctx)
# I think Num.abs is too restrictive, it should be able to produce a natural number, but it seem to be restricted to signed numbers.
size = List.len popCtx.stack - 1
offset = Num.intCast size - index
if offset >= 0 then
stackVal <- Result.after (List.get popCtx.stack (Num.intCast offset))
stackVal <- Result.try (List.get popCtx.stack (Num.intCast offset))
Ok (Context.pushStack popCtx stackVal)
else
Err OutOfBounds
@ -419,9 +419,9 @@ stepExecCtx = \ctx, char ->
# Due to possible division by zero error, this must be handled specially.
Task.fromResult
(
(T popCtx1 numR) <- Result.after (popNumber ctx)
(T popCtx2 numL) <- Result.after (popNumber popCtx1)
res <- Result.after (Num.divTruncChecked numL numR)
(T popCtx1 numR) <- Result.try (popNumber ctx)
(T popCtx2 numL) <- Result.try (popNumber popCtx1)
res <- Result.try (Num.divTruncChecked numL numR)
Ok (Context.pushStack popCtx2 (Number res))
)
@ -501,9 +501,9 @@ stepExecCtx = \ctx, char ->
# `:` store to variable
Task.fromResult
(
(T popCtx1 var) <- Result.after (popVariable ctx)
(T popCtx1 var) <- Result.try (popVariable ctx)
# The Result.mapErr on the next line maps from EmptyStack in Context.roc to the full InterpreterErrors union here.
(T popCtx2 n1) <- Result.after (Result.mapErr (Context.popStack popCtx1) (\EmptyStack -> EmptyStack))
(T popCtx2 n1) <- Result.try (Result.mapErr (Context.popStack popCtx1) (\EmptyStack -> EmptyStack))
Ok { popCtx2 & vars: List.set popCtx2.vars (Variable.toIndex var) n1 }
)
@ -511,8 +511,8 @@ stepExecCtx = \ctx, char ->
# `;` load from variable
Task.fromResult
(
(T popCtx var) <- Result.after (popVariable ctx)
elem <- Result.after (List.get popCtx.vars (Variable.toIndex var))
(T popCtx var) <- Result.try (popVariable ctx)
elem <- Result.try (List.get popCtx.vars (Variable.toIndex var))
Ok (Context.pushStack popCtx elem)
)
@ -548,13 +548,13 @@ stepExecCtx = \ctx, char ->
unaryOp : Context, (I32 -> I32) -> Result Context InterpreterErrors
unaryOp = \ctx, op ->
(T popCtx num) <- Result.after (popNumber ctx)
(T popCtx num) <- Result.try (popNumber ctx)
Ok (Context.pushStack popCtx (Number (op num)))
binaryOp : Context, (I32, I32 -> I32) -> Result Context InterpreterErrors
binaryOp = \ctx, op ->
(T popCtx1 numR) <- Result.after (popNumber ctx)
(T popCtx2 numL) <- Result.after (popNumber popCtx1)
(T popCtx1 numR) <- Result.try (popNumber ctx)
(T popCtx2 numL) <- Result.try (popNumber popCtx1)
Ok (Context.pushStack popCtx2 (Number (op numL numR)))
popNumber : Context -> Result [T Context I32] InterpreterErrors