more demo use

This commit is contained in:
Tom Elliott 2024-07-24 11:52:09 +12:00
parent fa5be168c0
commit dfaeabec05
4 changed files with 19 additions and 7 deletions

View File

@ -23,7 +23,7 @@ ts_function <- function(f, ..., result = NULL) {
}
fn <- function(...) {
mc <- match.call()
mc <- match.call(f)
x <- parse_args(args, mc)
result$check(do.call(f, x))
}

View File

@ -74,8 +74,10 @@ ts_numeric <- function(n = -1L) {
n_type(n, "number"),
n_type_fun(n, "Numeric"),
check = function(x) {
if (!is.numeric(x)) stop("Expected a number")
if (n > 0 && length(x) != n) stop("Expected a number of length ", n)
if (!is.numeric(x)) stop("Expected a number", call. = FALSE)
if (n > 0 && length(x) != n) {
stop("Expected a number of length ", n, , call. = FALSE)
}
x
}
)

View File

@ -94,9 +94,13 @@ type App = {
Here's what's currently working:
```{r}
```{r, error = TRUE}
library(ts)
myfun <- ts_function(mean, x = ts_numeric(), result = ts_numeric())
myfun <- ts_function(mean, x = ts_numeric(), result = ts_numeric(1))
myfun(1:5)
myfun("hello world")
ts_compile(myfun)
```

View File

@ -90,7 +90,13 @@ Heres whats currently working:
``` r
library(ts)
myfun <- ts_function(mean, x = ts_numeric(), result = ts_numeric())
myfun <- ts_function(mean, x = ts_numeric(), result = ts_numeric(1))
myfun(1:5)
#> [1] 3
myfun("hello world")
#> Error: Expected a number
ts_compile(myfun)
#> const myfun = (x: number | number[]) => Promise<Numeric>;
#> const myfun = (x: number | number[]) => Promise<Numeric<1>)>;
```