@@ -41,20 +41,32 @@ library(Rserve)
41
41
oc.init <- function () {
42
42
ocap(function () {
43
43
list (
44
+ # Ocaps are functions that javascript can call
44
45
add <- ocap(function (a , b ) {
45
46
a + b
46
47
}),
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
+ })
47
56
)
48
57
})
49
58
}
50
59
```
51
60
52
61
``` typescript
53
62
// ocap.ts
54
- import { function , number } from " rserve-ts/types" ;
63
+ import { function , number , list , ocap } from " rserve-ts/types" ;
55
64
56
65
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
+ })),
58
70
};
59
71
```
60
72
@@ -71,5 +83,9 @@ import { appFuns } from "./ocap";
71
83
72
84
const { data : sum } = await app .add (1 , 2 );
73
85
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 );
74
90
})();
75
91
```
0 commit comments