Use unwrap in some io functions

This commit is contained in:
imaqtkatt 2024-08-07 10:45:08 -03:00
parent ff7ff11b26
commit 7280819bf3
2 changed files with 47 additions and 16 deletions

View File

@ -118,6 +118,22 @@ Splits a list into two lists at the first occurrence of a value.
List/split_once(xs: List(T), val: T) -> (Result(List(T), List(T)))
```
## Result
```python
type Result<A, B>:
Ok { val: A }
Err { val: B }
```
### Result/unwrap
Returns the inner value of `Result/Ok` or `Result/Err`.
```python
def Result/unwrap(result: Result<A, B>): A || B
```
## Tree
```python
@ -527,21 +543,23 @@ def IO/DyLib/open(path: String, lazy: u24) -> u24
```
Loads a dynamic library file.
* `path` is the path to the library file.
* `lazy` is a boolean encoded as a `u24` that determines if all functions are loaded lazily (`1`) or upfront (`0`).
* Returns an unique id to the library object encoded as a `u24`.
- `path` is the path to the library file.
- `lazy` is a boolean encoded as a `u24` that determines if all functions are loaded lazily (`1`) or upfront (`0`).
- Returns an unique id to the library object encoded as a `u24`.
#### IO/DyLib/call
``` py
```py
def IO/DyLib/call(dl: u24, fn: String, args: Any) -> Any
```
Calls a function of a previously opened library.
* `dl` is the id of the library object.
* `fn` is the name of the function in the library.
* `args` are the arguments to the function. The expected values depend on the called function.
* The returned value is determined by the called function.
- `dl` is the id of the library object.
- `fn` is the name of the function in the library.
- `args` are the arguments to the function. The expected values depend on the called function.
- The returned value is determined by the called function.
#### IO/DyLib/close
@ -550,8 +568,9 @@ def IO/DyLib/close(dl: u24) -> None
```
Closes a previously open library.
* `dl` is the id of the library object.
* Returns nothing (`*`).
- `dl` is the id of the library object.
- Returns nothing (`*`).
## Native number casting
@ -585,7 +604,7 @@ Casts any native number to an i24.
```py
def String/decode_utf8(bytes: [u24]) -> String
```
```
Decodes a sequence of bytes to a String using utf-8 encoding.
@ -647,7 +666,6 @@ Computes the arctangent of `y / x`.
Has the same behaviour as `atan2f` in the C math lib.
### Math/PI
Defines the Pi constant.
@ -781,4 +799,4 @@ def Math/round(n: f24) -> f24
You can force a function call to be evaluated lazily by wrapping it in a lazy thunk.
In Bend, this can be expressed as `lambda x: x(my_function, arg1, arg2, ...)`.
To evaluate the thunk, you can use the `undefer` function or apply `lambda x: x` to it.
To evaluate the thunk, you can use the `undefer` function or apply `lambda x: x` to it.

View File

@ -92,6 +92,11 @@ type Nat = (Succ ~pred) | (Zero)
type Result = (Ok val) | (Err val)
Result/unwrap res = match res {
Result/Ok: res.val;
Result/Err: res.val;
}
type Tree:
Node { ~left, ~right }
Leaf { value }
@ -189,7 +194,10 @@ IO/done_on_err done = done
## Time and sleep
# Returns a monotonically increasing nanosecond timestamp as an u48 encoded as a pair of u24s.
IO/get_time = (IO/Call IO/MAGIC "GET_TIME" * @x (IO/Done IO/MAGIC x))
IO/get_time = with IO {
ask res = (IO/Call IO/MAGIC "GET_TIME" * @x (IO/Done IO/MAGIC x))
(wrap (Result/unwrap res))
}
# Sleeps for the given number of nanoseconds, given by an u48 encoded as a pair of u24s.
IO/nanosleep hi_lo = (IO/Call IO/MAGIC "SLEEP" hi_lo @x (IO/Done IO/MAGIC x))
@ -199,7 +207,9 @@ def IO/sleep(seconds):
nanos = seconds * 1_000_000_000.0
lo = to_u24(nanos % 0x1_000_000.0)
hi = to_u24(nanos / 0x1_000_000.0)
return IO/nanosleep((hi, lo))
with IO:
res <- IO/nanosleep((hi, lo))
return wrap(Result/unwrap(res))
## File IO
@ -296,7 +306,10 @@ def IO/FS/write_file(path, bytes):
# IO/print(text: String) -> (IO *)
# Prints a string to stdout, encoding it with utf-8.
IO/print text = (IO/FS/write IO/FS/STDOUT (String/encode_utf8 text))
IO/print text = with IO {
ask res = (IO/FS/write IO/FS/STDOUT (String/encode_utf8 text))
(wrap (Result/unwrap res))
}
# IO/input() -> IO String
# Read characters from stdin until a newline is found.