forked from dbdahl/rscala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.Rout-FALSE-TRUE-2.10
398 lines (398 loc) · 11.2 KB
/
defs.Rout-FALSE-TRUE-2.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
> source("common.R",print.eval=TRUE)
# R version 3.4.0 (2017-04-21) # becker # FALSE # TRUE
>
>
> ####
>
>
> rng1 <- s %.~% "new scala.util.Random"
> rng1$nextDouble()
[1] 0.4576356
>
> s$r1 <- rng1
> rng2 <- s$r1
> rng2$nextDouble()
[1] 0.4708717
>
> rng3 <- s$.scala.util.Random$new()
> rng3$nextDouble()
[1] 0.3953106
>
> s$r3 <- rng3
> rng4 <- s$r3
> rng4$nextDouble()
[1] 0.05973262
>
> f <- rng4$nextDouble(.EVALUATE=FALSE)
> f()
[1] 0.5947277
>
>
> ####
>
>
> see <- "David"
> mkFunc1 <- function() {
+ see <- "B."
+ x <- "Milly"
+ print(environment())
+ r <- function(x1="Mack",x2="Bob") s %!% '
+ x1+" "+x2+" "+R.getS0("see")
+ '
+ r
+ }
> y1 <- mkFunc1()
<environment: 0x459e368>
> identical(y1("Lisa","Dahl"),"Lisa Dahl B.")
[1] TRUE
>
>
> mkFunc2 <- function() {
+ see <- "MMMM."
+ x <- "Milly"
+ r <- function(x="Mack",y="Bob") s %.!% '
+ x+" "+y+" "+R.getS0("see")
+ '
+ r
+ }
> y2 <- mkFunc2()
> identical(y2("Lisa","Dahl")$toString(),"Lisa Dahl MMMM.")
[1] TRUE
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 349676 18.7 592000 31.7 460000 24.6
Vcells 640237 4.9 1231220 9.4 1041801 8.0
>
>
>
> func1 <- function() {
+ see <- "Knudsen"
+ y1("Lisa","Dahl")
+ }
> identical(func1(),"Lisa Dahl B.")
[1] TRUE
>
>
> ####
>
>
> # Realistic timing
> system.time({
+ e <- function(x=0,y=4L,name="dog") s %!% '
+ name + " " + ( x + y )
+ '
+ scalaOptimize(e)
+ })
user system elapsed
0.000 0.000 0.294
>
> # Taking advantage of caching
> system.time({
+ f <- function(x=0,y=4L,name="dog") s %!% '
+ name + " " + ( x + y )
+ '
+ scalaOptimize(f)
+ })
user system elapsed
0.004 0.000 0.002
>
> ####
>
> nextDouble <- function(rng=s$null("scala.util.Random")) s %!% "rng.nextDouble()"
>
> mkRNG1 <- function() s %.!% 'new scala.util.Random()'
> mkRNG2 <- function() s %.~% 'new scala.util.Random()'
>
> rng1 <- mkRNG1()
> rng2 <- mkRNG2()
>
> rng1$nextInt(10L)
[1] 7
> rng2$nextInt(10L)
[1] 3
>
> str <- rng1$toString(.AS.REFERENCE=TRUE)
> str$length()
[1] 25
>
> nd0 <- rng1$nextDouble(.EVALUATE=FALSE)
> nd1 <- function() s %!% 'R.cached("@{toString(rng1)}").asInstanceOf[@{rng1[[\'type\']]}].nextDouble()'
> nd1b <- scalaOptimize(nd1)
> nd2 <- function() s %!% '@{rng2}.nextDouble()'
> nd2b <- scalaOptimize(nd2)
>
> library("microbenchmark")
>
> microbenchmark(
+ runif(1),
+ rng1$nextDouble(),
+ rng2$nextDouble(),
+ nd0(),
+ nd1(),
+ nd1b(),
+ nd2(),
+ nd2b(),
+ times=500
+ )
Unit: microseconds
expr min lq mean median uq max
runif(1) 1.921 10.8890 13.85577 14.6030 16.8925 111.086
rng1$nextDouble() 1405.707 2328.4285 3821.91463 2635.5985 2874.5345 78220.199
rng2$nextDouble() 1285.477 2065.4455 3630.88791 2426.1510 2587.6040 228371.342
nd0() 220.776 459.5885 972.50019 520.1530 583.1775 83178.553
nd1() 1282.279 2040.1225 3331.45840 2440.1305 2644.0730 68618.636
nd1b() 250.910 431.3465 822.28818 515.0835 584.0370 39761.711
nd2() 1191.665 1942.4355 3356.28358 2259.1370 2428.9435 48670.817
nd2b() 248.918 422.9215 750.79199 499.7910 550.5840 34747.555
neval
500
500
500
500
500
500
500
500
>
> ####
>
> f <- function(x=4) {
+ if ( x < 0 ) stop("'x' must be positive")
+ if ( x == 0 ) x <- 10
+ s %!% '2*x'
+ }
>
> f(4)
[1] 8
> tryCatch(f(-3), error=function(e) e)
<simpleError in f(-3): 'x' must be positive>
> f(0)
[1] 20
>
> g <- scalaOptimize(f)
> g(4)
[1] 8
> g(-3) ## Note that no error because R code as been optimized away!
[1] -6
> g(0) ## And we don't get the special case when x==0.
[1] 0
>
> h <- function(x=4) {
+ if ( x < 0 ) stop("'x' must be positive")
+ if ( x == 0 ) x <- 10
+ s %~% '2*R.getD0("x")'
+ }
>
> h(4)
[1] 8
> tryCatch(h(-3), error=function(e) e)
<simpleError in h(-3): 'x' must be positive>
> h(0)
[1] 20
>
> library(microbenchmark)
> microbenchmark(
+ f(4),
+ g(4),
+ h(4), # h is *slow* and it has the memory leak inherent in Scala's REPL.
+ times=100)
Unit: microseconds
expr min lq mean median uq max neval
f(4) 1317.404 2309.782 3718.810 2556.908 2736.063 19686.77 100
g(4) 346.887 638.018 2541.977 730.943 888.745 50317.02 100
h(4) 42779.113 84515.135 114419.549 112684.507 128427.701 258837.10 100
>
> microbenchmark(
+ f(4),
+ g(4), # g is faster, but you lose the checking and special behavior.
+ times=1000)
Unit: microseconds
expr min lq mean median uq max neval
f(4) 1329.982 2142.970 3125.038 2417.075 2600.886 126584.37 1000
g(4) 365.769 608.244 1079.115 697.810 765.218 75318.21 1000
>
>
>
>
> ####
>
> s$.scala.util.Random$nextDouble()
[1] 0.5465692
> m <- s$.scala.util.Random$new(342L,.EVALUATE=FALSE)
> m(23436)$nextDouble()
[1] 0.8011135
> m(63502)$nextDouble()
[1] 0.1435077
> m(93222)$nextDouble()
[1] 0.4889048
> m(93222)$nextDouble()
[1] 0.4889048
>
> n <- m(5)$nextDouble(.EVALUATE=FALSE)
> n()
[1] 0.7305199
> n()
[1] 0.08825841
> n()
[1] 0.4889045
>
> s$'.Array[Int]'$new(5L)
[1] 0 0 0 0 0
>
> ####
>
> s$a <- 1:10
> a <- s$.a
> a$apply(3L)
[1] 4
>
> s$.scala.util.Random$nextDouble()
[1] 0.8169806
> s$.scala.util.Random$nextDouble()
[1] 0.9093313
>
> s %@% 'import scala.util.Random'
> s$.Random$nextDouble()
[1] 0.9592286
>
> a <- function() s %!% 'Random.nextDouble'
> a()
[1] 0.7001597
>
> a <- s$.scala.util.Random
> a$nextDouble()
[1] 0.8476964
>
> b <- ( function() s %!% 'scala.util.Random' )()
> b$nextDouble()
[1] 0.1955994
>
> library(microbenchmark)
> microbenchmark(
+ a$nextDouble(),
+ b$nextDouble(),
+ times=1000
+ )
Unit: milliseconds
expr min lq mean median uq max neval
a$nextDouble() 1.340784 2.078889 3.17287 2.453591 2.597942 83.57086 1000
b$nextDouble() 1.423608 2.227492 3.31518 2.624513 2.800624 40.00706 1000
>
>
> ####
>
> f <- function(x=NULL, wantNull=TRUE) s %.!% '
+ val r = R.makePersistent(x)
+ if ( wantNull ) null else r
+ '
>
> a <- f(1:10, FALSE)
> a$name()
[1] ".rsI[['r']]$Z1CyR8d4"
>
> g <- function(func=NULL,y=scalaNull("RPersistentReference")) s %!% '
+ R.invoke(func,y)
+ '
>
> g(print, a)
[1] 1 2 3 4 5 6 7 8 9 10
>
>
> ####
>
> f <- function(x=scalaNull("(Int,Int)")) s %!% 'x._1 + x._2'
> g <- s %~% "(300,400)"
> f(g)
[1] 700
> f(s %.~% "(30,40)")
[1] 70
>
> f2 <- function() s %~% 'println("Yes")'
> f2()
Yes
> capture.output(f2())
Yes
character(0)
>
> a <- s %.~% "(300,234)"
> f1 <- function(x=scalaNull("(Int,Int)"),y=numeric()) s %!% 'x._1 + x._2 + y.sum'
> f1(a,c(2,3,4,6))
[1] 549
>
> f1 <- function(x=scalaNull("(Int,Int)"),y=scalaNull("Array[Double]")) s %!% 'x._1 + x._2 + y.sum'
> b <- s %.~% "Array[Double](2,3,4,5)"
> f1(a,b)
[1] 548
>
> ####
>
> (function() s %!% 'println("Yes")')()
Yes
> (function() s %!% '0')()
[1] 0
> (function() s %!% 'null')()
>
> ####
>
> tryCatch((function() s %!% 'a+b')(),error = function(e) e)
<console>:14: error: not found: value b
a+b}
^
<simpleError in scalaEval(interpreter, snippet, workspace): Error in evaluation.>
> tryCatch((function() s %!% 'a+')(),error = function(e) e)
<console>:14: error: missing arguments for method + in class StringAdd;
follow this method with `_' if you want to treat it as a partially applied function
a+}
^
<simpleError in scalaEval(interpreter, snippet, workspace): Error in evaluation.>
> tryCatch((function() s %!% 'import org.asdfad')(),error = function(e) {e})
<console>:13: error: object asdfad is not a member of package org
import org.asdfad}
^
<simpleError in scalaEval(interpreter, snippet, workspace): Error in evaluation.>
> tryCatch((function() s %!% 'throw new RuntimeException()')(),error = function(e) {e})
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.ddahl.rscala.server.ScalaServer.doInvoke(ScalaServer.scala:128)
at org.ddahl.rscala.server.ScalaServer.org$ddahl$rscala$server$ScalaServer$$heart(ScalaServer.scala:512)
at org.ddahl.rscala.server.ScalaServer.run(ScalaServer.scala:539)
at org.ddahl.rscala.server.Main$delayedInit$body.apply(Main.scala:5)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at org.ddahl.rscala.server.Main$.main(Main.scala:3)
at org.ddahl.rscala.server.Main.main(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:71)
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:139)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:71)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:139)
at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45)
at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:74)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.RuntimeException
at $line322.$read$$iw$$iw$$anonfun$1.apply(<console>:13)
at $line322.$read$$iw$$iw$$anonfun$1.apply(<console>:12)
... 33 more
java.lang.reflect.InvocationTargetException
java.lang.RuntimeException
<simpleError in (structure(function () { .rsI <- .rsWY7CgMMwi .rsWorkspace <- environment() rscala:::wb(.rsI, rscala:::INVOKE) rscala:::wc(.rsI, "res154") flush(.rsI[["socketIn"]]) rscala:::rServe(.rsI, TRUE, .rsWorkspace) .rsStatus <- rscala:::rb(.rsI, "integer") if (.rsStatus == rscala:::ERROR) { stop("Invocation error.") } else { .rsResult <- rscala:::scalaGet(.rsI, "?", NA) if (is.null(.rsResult)) invisible(.rsResult) else .rsResult }}, identifiers = character(0), scalaHeader = character(0), scalaBody = "throw new RuntimeException()", returnType = "Nothing", asReference = NA, class = "ScalaFunction"))(): Invocation error.>
> s %~% "5+6" # Everything's still okay!
[1] 11
>
>