mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-28 22:48:07 +00:00
Rename Result.after to Result.try
This commit is contained in:
parent
31f7d72ce0
commit
0acab0eef3
5 changed files with 39 additions and 39 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
interface Result
|
interface Result
|
||||||
exposes [Result, isOk, isErr, map, mapErr, after, afterErr, withDefault]
|
exposes [Result, isOk, isErr, map, mapErr, try, afterErr, withDefault]
|
||||||
imports [Bool.{ Bool }]
|
imports [Bool.{ Bool }]
|
||||||
|
|
||||||
## The result of an operation that could fail: either the operation went
|
## 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`.)
|
## (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
|
## >>> Result.try (Err "yipes!") \num -> if num < 0 then Err "negative!" else Ok -num
|
||||||
after : Result a err, (a -> Result b err) -> Result b err
|
try : Result a err, (a -> Result b err) -> Result b err
|
||||||
after = \result, transform ->
|
try = \result, transform ->
|
||||||
when result is
|
when result is
|
||||||
Ok v -> transform v
|
Ok v -> transform v
|
||||||
Err e -> Err e
|
Err e -> Err e
|
||||||
|
|
@ -84,7 +84,7 @@ after = \result, transform ->
|
||||||
## If the result is `Err`, transform the entire result by running a conversion
|
## 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.
|
## 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
|
## >>> Result.afterErr (Ok 10) \errorNum -> Str.toNat errorNum
|
||||||
##
|
##
|
||||||
|
|
|
||||||
|
|
@ -291,7 +291,7 @@ fn decode() {
|
||||||
fromBytes = \lst, fmt ->
|
fromBytes = \lst, fmt ->
|
||||||
when decodeWith lst decoder fmt is
|
when decodeWith lst decoder fmt is
|
||||||
{ result, rest } ->
|
{ result, rest } ->
|
||||||
Result.after result \val ->
|
Result.try result \val ->
|
||||||
if List.isEmpty rest
|
if List.isEmpty rest
|
||||||
then Ok val
|
then Ok val
|
||||||
else Err (Leftover rest)
|
else Err (Leftover rest)
|
||||||
|
|
|
||||||
|
|
@ -2479,9 +2479,9 @@ fn backpassing_result() {
|
||||||
main : I64
|
main : I64
|
||||||
main =
|
main =
|
||||||
helper =
|
helper =
|
||||||
x <- Result.after a
|
x <- Result.try a
|
||||||
y <- Result.after (f x)
|
y <- Result.try (f x)
|
||||||
z <- Result.after (g y)
|
z <- Result.try (g y)
|
||||||
|
|
||||||
Ok z
|
Ok z
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -277,7 +277,7 @@ fn roc_result_after_on_ok() {
|
||||||
input : Result I64 Str
|
input : Result I64 Str
|
||||||
input = Ok 1
|
input = Ok 1
|
||||||
|
|
||||||
Result.after input \num ->
|
Result.try input \num ->
|
||||||
if num < 0 then Err "negative!" else Ok -num
|
if num < 0 then Err "negative!" else Ok -num
|
||||||
"#),
|
"#),
|
||||||
RocResult::ok(-1),
|
RocResult::ok(-1),
|
||||||
|
|
@ -293,7 +293,7 @@ fn roc_result_after_on_err() {
|
||||||
input : Result I64 Str
|
input : Result I64 Str
|
||||||
input = (Err "already a string")
|
input = (Err "already a string")
|
||||||
|
|
||||||
Result.after input \num ->
|
Result.try input \num ->
|
||||||
if num < 0 then Err "negative!" else Ok -num
|
if num < 0 then Err "negative!" else Ok -num
|
||||||
"#),
|
"#),
|
||||||
RocResult::err(RocStr::from("already a string")),
|
RocResult::err(RocStr::from("already a string")),
|
||||||
|
|
@ -308,7 +308,7 @@ fn roc_result_after_err() {
|
||||||
r#"
|
r#"
|
||||||
result : Result Str I64
|
result : Result Str I64
|
||||||
result =
|
result =
|
||||||
Result.afterErr (Ok "already a string") \num ->
|
Result.tryErr (Ok "already a string") \num ->
|
||||||
if num < 0 then Ok "negative!" else Err -num
|
if num < 0 then Ok "negative!" else Err -num
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
@ -321,7 +321,7 @@ fn roc_result_after_err() {
|
||||||
r#"
|
r#"
|
||||||
result : Result Str I64
|
result : Result Str I64
|
||||||
result =
|
result =
|
||||||
Result.afterErr (Err 100) \num ->
|
Result.tryErr (Err 100) \num ->
|
||||||
if num < 0 then Ok "negative!" else Err -num
|
if num < 0 then Ok "negative!" else Err -num
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
|
||||||
|
|
@ -239,13 +239,13 @@ interpretCtxLoop = \ctx ->
|
||||||
when result is
|
when result is
|
||||||
Ok (T 0xB8 newCtx) ->
|
Ok (T 0xB8 newCtx) ->
|
||||||
result2 =
|
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.
|
# 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
|
size = List.len popCtx.stack - 1
|
||||||
offset = Num.intCast size - index
|
offset = Num.intCast size - index
|
||||||
|
|
||||||
if offset >= 0 then
|
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)
|
Ok (Context.pushStack popCtx stackVal)
|
||||||
else
|
else
|
||||||
Err OutOfBounds
|
Err OutOfBounds
|
||||||
|
|
@ -289,7 +289,7 @@ stepExecCtx = \ctx, char ->
|
||||||
# `!` execute lambda
|
# `!` execute lambda
|
||||||
Task.fromResult
|
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 } }
|
Ok { popCtx & scopes: List.append popCtx.scopes { data: None, buf: bytes, index: 0, whileInfo: None } }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -297,8 +297,8 @@ stepExecCtx = \ctx, char ->
|
||||||
# `?` if
|
# `?` if
|
||||||
Task.fromResult
|
Task.fromResult
|
||||||
(
|
(
|
||||||
(T popCtx1 bytes) <- Result.after (popLambda ctx)
|
(T popCtx1 bytes) <- Result.try (popLambda ctx)
|
||||||
(T popCtx2 n1) <- Result.after (popNumber popCtx1)
|
(T popCtx2 n1) <- Result.try (popNumber popCtx1)
|
||||||
if n1 == 0 then
|
if n1 == 0 then
|
||||||
Ok popCtx2
|
Ok popCtx2
|
||||||
else
|
else
|
||||||
|
|
@ -309,8 +309,8 @@ stepExecCtx = \ctx, char ->
|
||||||
# `#` while
|
# `#` while
|
||||||
Task.fromResult
|
Task.fromResult
|
||||||
(
|
(
|
||||||
(T popCtx1 body) <- Result.after (popLambda ctx)
|
(T popCtx1 body) <- Result.try (popLambda ctx)
|
||||||
(T popCtx2 cond) <- Result.after (popLambda popCtx1)
|
(T popCtx2 cond) <- Result.try (popLambda popCtx1)
|
||||||
last = (List.len popCtx2.scopes - 1)
|
last = (List.len popCtx2.scopes - 1)
|
||||||
|
|
||||||
when List.get popCtx2.scopes last is
|
when List.get popCtx2.scopes last is
|
||||||
|
|
@ -343,8 +343,8 @@ stepExecCtx = \ctx, char ->
|
||||||
0x5C ->
|
0x5C ->
|
||||||
# `\` swap
|
# `\` swap
|
||||||
result2 =
|
result2 =
|
||||||
(T popCtx1 n1) <- Result.after (Context.popStack ctx)
|
(T popCtx1 n1) <- Result.try (Context.popStack ctx)
|
||||||
(T popCtx2 n2) <- Result.after (Context.popStack popCtx1)
|
(T popCtx2 n2) <- Result.try (Context.popStack popCtx1)
|
||||||
Ok (Context.pushStack (Context.pushStack popCtx2 n1) n2)
|
Ok (Context.pushStack (Context.pushStack popCtx2 n1) n2)
|
||||||
|
|
||||||
when result2 is
|
when result2 is
|
||||||
|
|
@ -358,9 +358,9 @@ stepExecCtx = \ctx, char ->
|
||||||
0x40 ->
|
0x40 ->
|
||||||
# `@` rot
|
# `@` rot
|
||||||
result2 =
|
result2 =
|
||||||
(T popCtx1 n1) <- Result.after (Context.popStack ctx)
|
(T popCtx1 n1) <- Result.try (Context.popStack ctx)
|
||||||
(T popCtx2 n2) <- Result.after (Context.popStack popCtx1)
|
(T popCtx2 n2) <- Result.try (Context.popStack popCtx1)
|
||||||
(T popCtx3 n3) <- Result.after (Context.popStack popCtx2)
|
(T popCtx3 n3) <- Result.try (Context.popStack popCtx2)
|
||||||
Ok (Context.pushStack (Context.pushStack (Context.pushStack popCtx3 n2) n1) n3)
|
Ok (Context.pushStack (Context.pushStack (Context.pushStack popCtx3 n2) n1) n3)
|
||||||
|
|
||||||
when result2 is
|
when result2 is
|
||||||
|
|
@ -381,13 +381,13 @@ stepExecCtx = \ctx, char ->
|
||||||
# `O` also treat this as pick for easier script writing
|
# `O` also treat this as pick for easier script writing
|
||||||
Task.fromResult
|
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.
|
# 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
|
size = List.len popCtx.stack - 1
|
||||||
offset = Num.intCast size - index
|
offset = Num.intCast size - index
|
||||||
|
|
||||||
if offset >= 0 then
|
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)
|
Ok (Context.pushStack popCtx stackVal)
|
||||||
else
|
else
|
||||||
Err OutOfBounds
|
Err OutOfBounds
|
||||||
|
|
@ -419,9 +419,9 @@ stepExecCtx = \ctx, char ->
|
||||||
# Due to possible division by zero error, this must be handled specially.
|
# Due to possible division by zero error, this must be handled specially.
|
||||||
Task.fromResult
|
Task.fromResult
|
||||||
(
|
(
|
||||||
(T popCtx1 numR) <- Result.after (popNumber ctx)
|
(T popCtx1 numR) <- Result.try (popNumber ctx)
|
||||||
(T popCtx2 numL) <- Result.after (popNumber popCtx1)
|
(T popCtx2 numL) <- Result.try (popNumber popCtx1)
|
||||||
res <- Result.after (Num.divTruncChecked numL numR)
|
res <- Result.try (Num.divTruncChecked numL numR)
|
||||||
Ok (Context.pushStack popCtx2 (Number res))
|
Ok (Context.pushStack popCtx2 (Number res))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -501,9 +501,9 @@ stepExecCtx = \ctx, char ->
|
||||||
# `:` store to variable
|
# `:` store to variable
|
||||||
Task.fromResult
|
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.
|
# 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 }
|
Ok { popCtx2 & vars: List.set popCtx2.vars (Variable.toIndex var) n1 }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -511,8 +511,8 @@ stepExecCtx = \ctx, char ->
|
||||||
# `;` load from variable
|
# `;` load from variable
|
||||||
Task.fromResult
|
Task.fromResult
|
||||||
(
|
(
|
||||||
(T popCtx var) <- Result.after (popVariable ctx)
|
(T popCtx var) <- Result.try (popVariable ctx)
|
||||||
elem <- Result.after (List.get popCtx.vars (Variable.toIndex var))
|
elem <- Result.try (List.get popCtx.vars (Variable.toIndex var))
|
||||||
Ok (Context.pushStack popCtx elem)
|
Ok (Context.pushStack popCtx elem)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -548,13 +548,13 @@ stepExecCtx = \ctx, char ->
|
||||||
|
|
||||||
unaryOp : Context, (I32 -> I32) -> Result Context InterpreterErrors
|
unaryOp : Context, (I32 -> I32) -> Result Context InterpreterErrors
|
||||||
unaryOp = \ctx, op ->
|
unaryOp = \ctx, op ->
|
||||||
(T popCtx num) <- Result.after (popNumber ctx)
|
(T popCtx num) <- Result.try (popNumber ctx)
|
||||||
Ok (Context.pushStack popCtx (Number (op num)))
|
Ok (Context.pushStack popCtx (Number (op num)))
|
||||||
|
|
||||||
binaryOp : Context, (I32, I32 -> I32) -> Result Context InterpreterErrors
|
binaryOp : Context, (I32, I32 -> I32) -> Result Context InterpreterErrors
|
||||||
binaryOp = \ctx, op ->
|
binaryOp = \ctx, op ->
|
||||||
(T popCtx1 numR) <- Result.after (popNumber ctx)
|
(T popCtx1 numR) <- Result.try (popNumber ctx)
|
||||||
(T popCtx2 numL) <- Result.after (popNumber popCtx1)
|
(T popCtx2 numL) <- Result.try (popNumber popCtx1)
|
||||||
Ok (Context.pushStack popCtx2 (Number (op numL numR)))
|
Ok (Context.pushStack popCtx2 (Number (op numL numR)))
|
||||||
|
|
||||||
popNumber : Context -> Result [T Context I32] InterpreterErrors
|
popNumber : Context -> Result [T Context I32] InterpreterErrors
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue