Skip to content

Commit e2f6358

Browse files
committed
readme
1 parent e684849 commit e2f6358

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,32 @@ library(Rserve)
4141
oc.init <- function() {
4242
ocap(function() {
4343
list(
44+
# Ocaps are functions that javascript can call
4445
add <- ocap(function(a, b) {
4546
a + b
4647
}),
48+
dist <- ocap(function(which = c('normal', 'uniform')) {
49+
# Ocaps can return new ocaps, too!
50+
# This could be useful for progressively revealing functionality, etc.
51+
switch(which,
52+
normal = list(sample = ocap(function(n) rnorm(n))),
53+
uniform = list(sample = ocap(function(n) runif(n)))
54+
)
55+
})
4756
)
4857
})
4958
}
5059
```
5160

5261
```typescript
5362
// ocap.ts
54-
import { function, number } from "rserve-ts/types";
63+
import { function, number, list, ocap } from "rserve-ts/types";
5564

5665
export const appFuns = {
57-
add: types.function([types.number(), types.number()], types.number()),
66+
add: ocap([z.number(), z.number()], number()),
67+
dist: ocap([z.enum(["normal", "uniform"])], list({
68+
sample: ocap([z.number()], numeric()),
69+
})),
5870
};
5971
```
6072

@@ -71,5 +83,9 @@ import { appFuns } from "./ocap";
7183

7284
const { data: sum } = await app.add(1, 2);
7385
console.log("1 + 2 = ", sum);
86+
87+
const { data: normal } = await app.dist("normal");
88+
const { data: sample } = await normal.sample(5);
89+
console.log("Normal sample: ", sample);
7490
})();
7591
```

0 commit comments

Comments
 (0)