From 2473cee19810d64ca9a91f8a4a738fad554d88b3 Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Fri, 12 Jul 2024 18:04:45 +1200 Subject: [PATCH] readme --- .Rbuildignore | 1 + README.Rmd | 86 +++++++ README.html | 662 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 81 ++++++ 4 files changed, 830 insertions(+) create mode 100644 README.Rmd create mode 100644 README.html create mode 100644 README.md diff --git a/.Rbuildignore b/.Rbuildignore index 5163d0b..2a2cb83 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1 +1,2 @@ ^LICENSE\.md$ +^README\.Rmd$ diff --git a/README.Rmd b/README.Rmd new file mode 100644 index 0000000..7437b8e --- /dev/null +++ b/README.Rmd @@ -0,0 +1,86 @@ +--- +output: github_document +--- + + + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + fig.path = "man/figures/README-", + out.width = "100%" +) +``` + +# ts + + + + +The **ts** package makes it easy for users to write functions that can be used in **rserve-ts** applications. + +## Installation + +You can install the development version of ts from [GitHub](https://github.com/) with: + +``` r +# install.packages("devtools") +devtools::install_github("tmelliott/ts") +``` + +## Example + +Writing functions is easy, just use the `ts::x()` 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()) + } + ), + 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() + ) + ) + } + ) +) + +ts::compile(app) +``` + +This will generate the following rserve-ts function definitions: +```typescript +import { types as R } from "rserve-ts"; + +export const app = { + add: z.function( + z.tuple([z.number(), z.number()]), + z.promise(R.numeric(1)) + ), + sample: z.function( + z.tuple([z.character_vector(), z.integer()]), + z.promise(z.union([z.character(), z.character_vector()])) + ) +}; +``` + +which will generate the following types: +```typescript +type App = { + add: (x: number, y: number) => Promise; + sample: (x: string[], n: number) => Promise; + // or, if possible, even better: + sample: (x: string[], n: N) => Promise; +}; +``` diff --git a/README.html b/README.html new file mode 100644 index 0000000..b520161 --- /dev/null +++ b/README.html @@ -0,0 +1,662 @@ + + + + + + + + + + + + + + + + + + + + + +

ts

+ + + + +

The ts package makes it easy for users to write functions that can be used in rserve-ts applications.

+

Installation

+

You can install the development version of ts from GitHub with:

+
# install.packages("devtools")
+devtools::install_github("tmelliott/ts")
+

Example

+

Writing functions is easy, just use the ts::x() 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.

+
app <- ts::app(
+  add = ts::fun(
+    function(x = ts::number(), y = ts::number()) {
+      result <- x + y
+      ts::result(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()
+        )
+      )
+    }
+  )
+)
+
+ts::compile(app)
+

This will generate the following rserve-ts function definitions:

+
import { types as R } from "rserve-ts";
+
+export const app = {
+  add: z.function(
+    z.tuple([z.number(), z.number()]),
+    z.promise(R.numeric(1))
+  ),
+  sample: z.function(
+    z.tuple([z.character_vector(), z.integer()]),
+    z.promise(z.union([z.character(), z.character_vector()]))
+  )
+};
+

which will generate the following types:

+
type App = {
+  add: (x: number, y: number) => Promise<number>;
+  sample: (x: string[], n: number) => Promise<string | string[]>;
+  // or, if possible, even better:
+  sample: <N extends number>(x: string[], n: N) => Promise<N extends 1 ? string : string[]>;
+};
+ + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..9636347 --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ + + + +# ts + + + + + +The **ts** package makes it easy for users to write functions that can +be used in **rserve-ts** applications. + +## Installation + +You can install the development version of ts from +[GitHub](https://github.com/) with: + +``` r +# install.packages("devtools") +devtools::install_github("tmelliott/ts") +``` + +## Example + +Writing functions is easy, just use the `ts::x()` 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()) + } + ), + 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() + ) + ) + } + ) +) + +ts::compile(app) +``` + +This will generate the following rserve-ts function definitions: + +``` typescript +import { types as R } from "rserve-ts"; + +export const app = { + add: z.function( + z.tuple([z.number(), z.number()]), + z.promise(R.numeric(1)) + ), + sample: z.function( + z.tuple([z.character_vector(), z.integer()]), + z.promise(z.union([z.character(), z.character_vector()])) + ) +}; +``` + +which will generate the following types: + +``` typescript +type App = { + add: (x: number, y: number) => Promise; + sample: (x: string[], n: number) => Promise; + // or, if possible, even better: + sample: (x: string[], n: N) => Promise; +}; +```