basic working example

This commit is contained in:
Tom Elliott 2024-07-24 10:24:46 +12:00
parent c19b73ea6d
commit bb8b2323e8
8 changed files with 226 additions and 703 deletions

13
Makefile Normal file
View File

@ -0,0 +1,13 @@
default: README.md
rfiles: $(wildcard R/*.R)
document:
Rscript -e "devtools::document()"
install: document
R CMD INSTALL .
README.md: README.Rmd install
Rscript -e "rmarkdown::render('README.Rmd')"
@rm README.html

View File

@ -1,2 +1,9 @@
# Generated by roxygen2: do not edit by hand
S3method(print,ts_object)
export(ts_character)
export(ts_compile)
export(ts_function)
export(ts_integer)
export(ts_logical)
export(ts_numeric)

11
R/compile.R Normal file
View File

@ -0,0 +1,11 @@
#' @export
ts_compile <- function(f) {
name <- deparse(substitute(f))
inputs <- attr(f, "args")
result <- attr(f, "result")
inputs <- sapply(inputs, \(x) x$type)
fn_args <- paste(names(inputs), inputs, sep = ": ") |>
paste(collapse = ", ")
cat(sprintf("const %s = (%s) => %s;", name, fn_args, result$type_fn), "\n")
}

34
R/function.R Normal file
View File

@ -0,0 +1,34 @@
parse_args <- function(x, mc) {
fmls <- lapply(x, eval)
mc <- mc[-1]
if (!all(names(mc) %in% names(fmls))) {
stop(
"Invalid argument(s): ",
paste(setdiff(names(mc), names(fmls)), collapse = ", ")
)
}
args <- lapply(names(fmls), function(n) {
fmls[[n]]$check(eval(mc[[n]]))
})
names(args) <- names(fmls)
args
}
# TS function
#' @export
ts_function <- function(f, ..., result = NULL) {
args <- list(...)
if (!is_object(result)) {
stop("Invalid return type")
}
fn <- function(...) {
mc <- match.call()
x <- parse_args(args, mc)
result$check(do.call(f, x))
}
attr(fn, "args") <- args
attr(fn, "result") <- result
class(fn) <- c("ts_function", class(f))
fn
}

92
R/types.R Normal file
View File

@ -0,0 +1,92 @@
object <- function(type = "any",
type_fn = "any",
default = NULL,
check = function() stop("Not implemented")) {
e <- environment()
e$attr <- function(name, value) {
e$attributes[[name]] <- value
}
class(e) <- c("ts_object", class(e))
e
}
#' @export
print.ts_object <- function(x, ...) {
# name <- deparse(substitute(x))
cat(sprintf("Object: %s\n", x$type))
cat(sprintf("rserve-ts type: %s\n", x$type_fn))
}
is_object <- function(x) {
inherits(x, "ts_object")
}
ts_union <- function(...) paste(..., sep = " | ")
ts_array <- function(type) paste(type, "[]", sep = "")
n_type <- function(n, type, pl = ts_array(type)) {
if (n == 1) {
return(type)
}
if (n == -1) {
return(ts_union(type, pl))
}
pl
}
n_type_fun <- function(n, type) {
sprintf("%s(%s)", type, ifelse(n < 0, "", n))
}
#' @export
ts_logical <- function(n = -1L) {
object(
n_type(n, "boolean"),
n_type_fun(n, "logical"),
check = function(x) {
if (!is.logical(x)) stop("Expected a boolean")
if (n > 0 && length(x) != n) stop("Expected a boolean of length ", n)
x
}
)
}
#' @export
ts_integer <- function(n = -1L) {
object(
n_type(n, "number"),
n_type_fun(n, "integer"),
check = function(x) {
if (!is.integer(x)) stop("Expected an integer")
if (n > 0 && length(x) != n) stop("Expected an integer of length ", n)
x
}
)
}
#' @export
ts_numeric <- function(n = -1L) {
object(
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)
x
}
)
}
#' @export
ts_character <- function(n = -1L) {
object(
n_type(n, "string"),
n_type_fun(n, "character"),
check = function(x) {
if (!is.character(x)) stop("Expected a string")
if (n > 0 && length(x) != n) stop("Expected a string of length ", n)
x
}
)
}

View File

@ -31,32 +31,36 @@ devtools::install_github("tmelliott/ts")
## Example
Writing functions is easy, just use the `ts::x()` functions to define formals and return types.
Writing functions is easy, just use the `ts_*()` functions to define formals and return types.
*Note: we recommend not importing the library, and instead using the fully qualified name `ts::x()` to avoid conflicts with other libraries.*
```r
app <- ts::app(
add = ts::fun(
function(x = ts::number(), y = ts::number()) {
result <- x + y
ts::result(result, ts::number())
}
library(ts)
app <- ts_app(
add = ts_fun(
function(x, y) {
x + y
},
x = ts_number(),
y = ts_number(),
# ideally this will use a generic type where x OR y can be vectors
# and, if one is a vector, the return type will be a vector too...
result = ts_number()
),
sample = ts::fun(
function(x = ts::character_vector(), n = ts::integer()) {
result <- sample(x, n)
ts::result(result,
ts::condition(n,
1 = ts::character(),
ts::character_vector()
sample = ts_fun(
function(x, n) {
sample(x, n)
},
x = ts_character_vector(),
n = ts_integer(),
result = ts_condition(n,
1 = ts_character(),
ts_character_vector()
)
)
}
)
)
ts::compile(app)
ts_compile(app)
```
This will generate the following rserve-ts function definitions:
@ -85,3 +89,14 @@ type App = {
Promise<{ data: N extends 1 ? string : string[] }>;
};
```
## State of the project
Here's what's currently working:
```{r}
library(ts)
myfun <- ts_function(mean, x = ts_numeric(), result = ts_numeric())
ts_compile(myfun)
```

View File

@ -1,663 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
@font-face {
font-family: octicons-link;
src: url(data:font/woff;charset=utf-8;base64,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) format('woff');
}
body {
-webkit-text-size-adjust: 100%;
text-size-adjust: 100%;
color: #333;
font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 16px;
line-height: 1.6;
word-wrap: break-word;
}
a {
background-color: transparent;
}
a:active,
a:hover {
outline: 0;
}
strong {
font-weight: bold;
}
h1 {
font-size: 2em;
margin: 0.67em 0;
}
img {
border: 0;
}
hr {
box-sizing: content-box;
height: 0;
}
pre {
overflow: auto;
}
code,
kbd,
pre {
font-family: monospace, monospace;
font-size: 1em;
}
input {
color: inherit;
font: inherit;
margin: 0;
}
html input[disabled] {
cursor: default;
}
input {
line-height: normal;
}
input[type="checkbox"] {
box-sizing: border-box;
padding: 0;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
td,
th {
padding: 0;
}
* {
box-sizing: border-box;
}
input {
font: 13px / 1.4 Helvetica, arial, nimbussansl, liberationsans, freesans, clean, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
a {
color: #4078c0;
text-decoration: none;
}
a:hover,
a:active {
text-decoration: underline;
}
hr {
height: 0;
margin: 15px 0;
overflow: hidden;
background: transparent;
border: 0;
border-bottom: 1px solid #ddd;
}
hr:before {
display: table;
content: "";
}
hr:after {
display: table;
clear: both;
content: "";
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 15px;
margin-bottom: 15px;
line-height: 1.1;
}
h1 {
font-size: 30px;
}
h2 {
font-size: 21px;
}
h3 {
font-size: 16px;
}
h4 {
font-size: 14px;
}
h5 {
font-size: 12px;
}
h6 {
font-size: 11px;
}
blockquote {
margin: 0;
}
ul,
ol {
padding: 0;
margin-top: 0;
margin-bottom: 0;
}
ol ol,
ul ol {
list-style-type: lower-roman;
}
ul ul ol,
ul ol ol,
ol ul ol,
ol ol ol {
list-style-type: lower-alpha;
}
dd {
margin-left: 0;
}
code {
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px;
}
pre {
margin-top: 0;
margin-bottom: 0;
font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
.select::-ms-expand {
opacity: 0;
}
.octicon {
font: normal normal normal 16px/1 octicons-link;
display: inline-block;
text-decoration: none;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.octicon-link:before {
content: '\f05c';
}
.markdown-body:before {
display: table;
content: "";
}
.markdown-body:after {
display: table;
clear: both;
content: "";
}
.markdown-body>*:first-child {
margin-top: 0 !important;
}
.markdown-body>*:last-child {
margin-bottom: 0 !important;
}
a:not([href]) {
color: inherit;
text-decoration: none;
}
.anchor {
display: inline-block;
padding-right: 2px;
margin-left: -18px;
}
.anchor:focus {
outline: none;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 1em;
margin-bottom: 16px;
font-weight: bold;
line-height: 1.4;
}
h1 .octicon-link,
h2 .octicon-link,
h3 .octicon-link,
h4 .octicon-link,
h5 .octicon-link,
h6 .octicon-link {
color: #000;
vertical-align: middle;
visibility: hidden;
}
h1:hover .anchor,
h2:hover .anchor,
h3:hover .anchor,
h4:hover .anchor,
h5:hover .anchor,
h6:hover .anchor {
text-decoration: none;
}
h1:hover .anchor .octicon-link,
h2:hover .anchor .octicon-link,
h3:hover .anchor .octicon-link,
h4:hover .anchor .octicon-link,
h5:hover .anchor .octicon-link,
h6:hover .anchor .octicon-link {
visibility: visible;
}
h1 {
padding-bottom: 0.3em;
font-size: 2.25em;
line-height: 1.2;
border-bottom: 1px solid #eee;
}
h1 .anchor {
line-height: 1;
}
h2 {
padding-bottom: 0.3em;
font-size: 1.75em;
line-height: 1.225;
border-bottom: 1px solid #eee;
}
h2 .anchor {
line-height: 1;
}
h3 {
font-size: 1.5em;
line-height: 1.43;
}
h3 .anchor {
line-height: 1.2;
}
h4 {
font-size: 1.25em;
}
h4 .anchor {
line-height: 1.2;
}
h5 {
font-size: 1em;
}
h5 .anchor {
line-height: 1.1;
}
h6 {
font-size: 1em;
color: #777;
}
h6 .anchor {
line-height: 1.1;
}
p,
blockquote,
ul,
ol,
dl,
table,
pre {
margin-top: 0;
margin-bottom: 16px;
}
hr {
height: 4px;
padding: 0;
margin: 16px 0;
background-color: #e7e7e7;
border: 0 none;
}
ul,
ol {
padding-left: 2em;
}
ul ul,
ul ol,
ol ol,
ol ul {
margin-top: 0;
margin-bottom: 0;
}
li>p {
margin-top: 16px;
}
dl {
padding: 0;
}
dl dt {
padding: 0;
margin-top: 16px;
font-size: 1em;
font-style: italic;
font-weight: bold;
}
dl dd {
padding: 0 16px;
margin-bottom: 16px;
}
blockquote {
padding: 0 15px;
color: #777;
border-left: 4px solid #ddd;
}
blockquote>:first-child {
margin-top: 0;
}
blockquote>:last-child {
margin-bottom: 0;
}
table {
display: block;
width: 100%;
overflow: auto;
word-break: normal;
word-break: keep-all;
}
table th {
font-weight: bold;
}
table th,
table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
table tr {
background-color: #fff;
border-top: 1px solid #ccc;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
img {
max-width: 100%;
box-sizing: content-box;
background-color: #fff;
}
code {
padding: 0;
padding-top: 0.2em;
padding-bottom: 0.2em;
margin: 0;
font-size: 85%;
background-color: rgba(0,0,0,0.04);
border-radius: 3px;
}
code:before,
code:after {
letter-spacing: -0.2em;
content: "\00a0";
}
pre>code {
padding: 0;
margin: 0;
font-size: 100%;
word-break: normal;
white-space: pre;
background: transparent;
border: 0;
}
.highlight {
margin-bottom: 16px;
}
.highlight pre,
pre {
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: #f7f7f7;
border-radius: 3px;
}
.highlight pre {
margin-bottom: 0;
word-break: normal;
}
pre {
word-wrap: normal;
}
pre code {
display: inline;
max-width: initial;
padding: 0;
margin: 0;
overflow: initial;
line-height: inherit;
word-wrap: normal;
background-color: transparent;
border: 0;
}
pre code:before,
pre code:after {
content: normal;
}
kbd {
display: inline-block;
padding: 3px 5px;
font-size: 11px;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: solid 1px #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb;
}
.pl-c {
color: #969896;
}
.pl-c1,
.pl-s .pl-v {
color: #0086b3;
}
.pl-e,
.pl-en {
color: #795da3;
}
.pl-s .pl-s1,
.pl-smi {
color: #333;
}
.pl-ent {
color: #63a35c;
}
.pl-k {
color: #a71d5d;
}
.pl-pds,
.pl-s,
.pl-s .pl-pse .pl-s1,
.pl-sr,
.pl-sr .pl-cce,
.pl-sr .pl-sra,
.pl-sr .pl-sre {
color: #183691;
}
.pl-v {
color: #ed6a43;
}
.pl-id {
color: #b52a1d;
}
.pl-ii {
background-color: #b52a1d;
color: #f8f8f8;
}
.pl-sr .pl-cce {
color: #63a35c;
font-weight: bold;
}
.pl-ml {
color: #693a17;
}
.pl-mh,
.pl-mh .pl-en,
.pl-ms {
color: #1d3e81;
font-weight: bold;
}
.pl-mq {
color: #008080;
}
.pl-mi {
color: #333;
font-style: italic;
}
.pl-mb {
color: #333;
font-weight: bold;
}
.pl-md {
background-color: #ffecec;
color: #bd2c00;
}
.pl-mi1 {
background-color: #eaffea;
color: #55a532;
}
.pl-mdr {
color: #795da3;
font-weight: bold;
}
.pl-mo {
color: #1d3e81;
}
kbd {
display: inline-block;
padding: 3px 5px;
font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: solid 1px #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb;
}
.task-list-item {
list-style-type: none;
}
.task-list-item+.task-list-item {
margin-top: 3px;
}
.task-list-item input {
margin: 0 0.35em 0.25em -1.6em;
vertical-align: middle;
}
:checked+.radio-label {
z-index: 1;
position: relative;
border-color: #4078c0;
}
.sourceLine {
display: inline-block;
}
code .kw { color: #000000; }
code .dt { color: #ed6a43; }
code .dv { color: #009999; }
code .bn { color: #009999; }
code .fl { color: #009999; }
code .ch { color: #009999; }
code .st { color: #183691; }
code .co { color: #969896; }
code .ot { color: #0086b3; }
code .al { color: #a61717; }
code .fu { color: #63a35c; }
code .er { color: #a61717; background-color: #e3d2d2; }
code .wa { color: #000000; }
code .cn { color: #008080; }
code .sc { color: #008080; }
code .vs { color: #183691; }
code .ss { color: #183691; }
code .im { color: #000000; }
code .va {color: #008080; }
code .cf { color: #000000; }
code .op { color: #000000; }
code .bu { color: #000000; }
code .ex { color: #000000; }
code .pp { color: #999999; }
code .at { color: #008080; }
code .do { color: #969896; }
code .an { color: #008080; }
code .cv { color: #008080; }
code .in { color: #008080; }
</style>
<style>
body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
padding-top: 0px;
}
</style>
</head>
<body>
<!-- README.md is generated from README.Rmd. Please edit that file -->
<h1 id="ts">ts</h1>
<!-- badges: start -->
<!-- badges: end -->
<p>The <strong>ts</strong> package makes it easy for users to write functions that can be used in <strong>rserve-ts</strong> applications.</p>
<h2 id="installation">Installation</h2>
<p>You can install the development version of ts from <a href="https://github.com/">GitHub</a> with:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="co"># install.packages(&quot;devtools&quot;)</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a>devtools<span class="op">::</span><span class="kw">install_github</span>(<span class="st">&quot;tmelliott/ts&quot;</span>)</span></code></pre></div>
<h2 id="example">Example</h2>
<p>Writing functions is easy, just use the <code>ts::x()</code> functions to define formals and return types.</p>
<p><em>Note: we recommend not importing the library, and instead using the fully qualified name <code>ts::x()</code> to avoid conflicts with other libraries.</em></p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a>app &lt;-<span class="st"> </span>ts<span class="op">::</span><span class="kw">app</span>(</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> <span class="dt">add =</span> ts<span class="op">::</span><span class="kw">fun</span>(</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a> <span class="cf">function</span>(<span class="dt">x =</span> ts<span class="op">::</span><span class="kw">number</span>(), <span class="dt">y =</span> ts<span class="op">::</span><span class="kw">number</span>()) {</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a> result &lt;-<span class="st"> </span>x <span class="op">+</span><span class="st"> </span>y</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a> ts<span class="op">::</span><span class="kw">result</span>(result, ts<span class="op">::</span><span class="kw">number</span>())</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a> }</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a> ),</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true"></a> <span class="dt">sample =</span> ts<span class="op">::</span><span class="kw">fun</span>(</span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true"></a> <span class="cf">function</span>(<span class="dt">x =</span> ts<span class="op">::</span><span class="kw">character_vector</span>(), <span class="dt">n =</span> ts<span class="op">::</span><span class="kw">integer</span>()) {</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true"></a> result &lt;-<span class="st"> </span><span class="kw">sample</span>(x, n)</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true"></a> ts<span class="op">::</span><span class="kw">result</span>(result,</span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true"></a> ts<span class="op">::</span><span class="kw">condition</span>(n,</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true"></a> <span class="dv">1</span> =<span class="st"> </span>ts<span class="op">::</span><span class="kw">character</span>(),</span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true"></a> ts<span class="op">::</span><span class="kw">character_vector</span>()</span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true"></a> )</span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true"></a> )</span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true"></a> }</span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true"></a> )</span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true"></a>)</span>
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true"></a></span>
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true"></a>ts<span class="op">::</span><span class="kw">compile</span>(app)</span></code></pre></div>
<p>This will generate the following rserve-ts function definitions:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="im">import</span> { types <span class="im">as</span> R } from <span class="st">&quot;rserve-ts&quot;</span><span class="op">;</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a>export const app <span class="op">=</span> {</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a> add<span class="op">:</span> z<span class="op">.</span><span class="fu">function</span>(</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a> z<span class="op">.</span><span class="fu">tuple</span>([z<span class="op">.</span><span class="fu">number</span>()<span class="op">,</span> z<span class="op">.</span><span class="fu">number</span>()])<span class="op">,</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a> z<span class="op">.</span><span class="fu">promise</span>(R<span class="op">.</span><span class="fu">numeric</span>(<span class="dv">1</span>))</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true"></a> )<span class="op">,</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true"></a> sample<span class="op">:</span> z<span class="op">.</span><span class="fu">function</span>(</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true"></a> z<span class="op">.</span><span class="fu">tuple</span>([z<span class="op">.</span><span class="fu">character_vector</span>()<span class="op">,</span> z<span class="op">.</span><span class="fu">integer</span>()])<span class="op">,</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true"></a> z<span class="op">.</span><span class="fu">promise</span>(R<span class="op">.</span><span class="fu">character</span>())</span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true"></a> )</span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true"></a>}<span class="op">;</span></span></code></pre></div>
<p>which will generate the following types:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode typescript"><code class="sourceCode typescript"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true"></a><span class="kw">type</span> App <span class="op">=</span> {</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true"></a> add<span class="op">:</span> (x<span class="op">:</span> <span class="dt">number</span><span class="op">,</span> y<span class="op">:</span> <span class="dt">number</span>) <span class="kw">=&gt;</span> <span class="bu">Promise</span><span class="op">&lt;</span>{ data<span class="op">:</span> <span class="dt">number</span> }<span class="op">&gt;;</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true"></a> sample<span class="op">:</span> (x<span class="op">:</span> <span class="dt">string</span>[]<span class="op">,</span> n<span class="op">:</span> <span class="dt">number</span>) <span class="kw">=&gt;</span> <span class="bu">Promise</span><span class="op">&lt;</span>{ data<span class="op">:</span> <span class="dt">string</span> <span class="op">|</span> <span class="dt">string</span>[] }<span class="op">&gt;;</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true"></a> <span class="co">// or, if possible, even better:</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true"></a> sample<span class="op">:</span> <span class="op">&lt;</span>N extends <span class="dt">number</span><span class="op">&gt;</span>(x<span class="op">:</span> <span class="dt">string</span>[]<span class="op">,</span> n<span class="op">:</span> N) <span class="kw">=&gt;</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true"></a> <span class="bu">Promise</span><span class="op">&lt;</span>{ data<span class="op">:</span> N extends <span class="dv">1</span> <span class="op">?</span> <span class="dt">string</span> <span class="op">:</span> <span class="dt">string</span>[] }<span class="op">&gt;;</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true"></a>}<span class="op">;</span></span></code></pre></div>
</body>
</html>

View File

@ -22,34 +22,36 @@ devtools::install_github("tmelliott/ts")
## Example
Writing functions is easy, just use the `ts::x()` functions to define
Writing functions is easy, just use the `ts_*()` functions to define
formals and return types.
*Note: we recommend not importing the library, and instead using the
fully qualified name `ts::x()` to avoid conflicts with other libraries.*
``` r
app <- ts::app(
add = ts::fun(
function(x = ts::number(), y = ts::number()) {
result <- x + y
ts::result(result, ts::number())
}
library(ts)
app <- ts_app(
add = ts_fun(
function(x, y) {
x + y
},
x = ts_number(),
y = ts_number(),
# ideally this will use a generic type where x OR y can be vectors
# and, if one is a vector, the return type will be a vector too...
result = ts_number()
),
sample = ts::fun(
function(x = ts::character_vector(), n = ts::integer()) {
result <- sample(x, n)
ts::result(result,
ts::condition(n,
1 = ts::character(),
ts::character_vector()
sample = ts_fun(
function(x, n) {
sample(x, n)
},
x = ts_character_vector(),
n = ts_integer(),
result = ts_condition(n,
1 = ts_character(),
ts_character_vector()
)
)
}
)
)
ts::compile(app)
ts_compile(app)
```
This will generate the following rserve-ts function definitions:
@ -80,3 +82,15 @@ type App = {
Promise<{ data: N extends 1 ? string : string[] }>;
};
```
## State of the project
Heres whats currently working:
``` r
library(ts)
myfun <- ts_function(mean, x = ts_numeric(), result = ts_numeric())
ts_compile(myfun)
#> const myfun = (x: number | number[]) => numeric();
```