forked from dbdahl/rscala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.Rout-FALSE-TRUE-2.12
404 lines (404 loc) · 11.5 KB
/
defs.Rout-FALSE-TRUE-2.12
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
399
400
401
402
403
404
> 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.4705106
>
> s$r1 <- rng1
> rng2 <- s$r1
> rng2$nextDouble()
[1] 0.08501808
>
> rng3 <- s$.scala.util.Random$new()
> rng3$nextDouble()
[1] 0.5303479
>
> s$r3 <- rng3
> rng4 <- s$r3
> rng4$nextDouble()
[1] 0.6078026
>
> f <- rng4$nextDouble(.EVALUATE=FALSE)
> f()
[1] 0.7679911
>
>
> ####
>
>
> 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: 0x3041fe8>
> 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.0 0.0 0.3
>
> # 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] 9
> rng2$nextInt(10L)
[1] 6
>
> str <- rng1$toString(.AS.REFERENCE=TRUE)
> str$length()
[1] 26
>
> 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.831 8.8905 11.44411 11.6615 14.1285 85.265
rng1$nextDouble() 1382.870 1582.8865 2781.82868 2486.9745 2702.2295 75444.034
rng2$nextDouble() 1272.437 1445.3755 2665.16389 2290.6880 2497.7125 202361.388
nd0() 215.030 284.3930 489.74339 430.8175 532.5355 28156.544
nd1() 1290.654 1477.3860 2301.71656 2278.1685 2507.0100 24406.377
nd1b() 207.313 293.6725 557.14161 458.8365 550.6435 15935.121
nd2() 1171.905 1377.6485 2294.60837 2128.6125 2311.2895 68218.716
nd2b() 221.214 281.9395 466.90246 437.1495 531.8385 10797.080
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) 1409.594 2116.8875 2949.9847 2510.177 2656.688 24445.17 100
g(4) 321.791 562.1225 935.6954 719.032 842.806 12620.65 100
h(4) 78827.902 110544.3965 138837.2899 132814.303 156437.883 271993.87 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) 1254.190 1444.284 2347.924 2075.7260 2424.162 73400.59 1000
g(4) 311.847 396.199 691.850 605.1775 714.887 27864.12 1000
>
>
>
>
> ####
>
> s$.scala.util.Random$nextDouble()
[1] 0.4137446
> 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.03597718
> s$.scala.util.Random$nextDouble()
[1] 0.7849089
>
> s %@% 'import scala.util.Random'
> s$.Random$nextDouble()
[1] 0.3303062
>
> a <- function() s %!% 'Random.nextDouble'
> a()
[1] 0.2844731
>
> a <- s$.scala.util.Random
> a$nextDouble()
[1] 0.2415158
>
> b <- ( function() s %!% 'scala.util.Random' )()
> b$nextDouble()
[1] 0.008949469
>
> library(microbenchmark)
> microbenchmark(
+ a$nextDouble(),
+ b$nextDouble(),
+ times=1000
+ )
Unit: milliseconds
expr min lq mean median uq max neval
a$nextDouble() 1.341008 2.310144 3.624497 2.464782 2.602102 173.63809 1000
b$nextDouble() 1.405923 2.481765 3.268970 2.649134 2.791162 34.35429 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']]$GuUUsgOE"
>
> 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>:18: 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>:18: error: missing argument list for method + in class any2stringadd
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `+ _` or `+(_)` instead of `+`.
a+}
^
<simpleError in scalaEval(interpreter, snippet, workspace): Error in evaluation.>
> tryCatch((function() s %!% 'import org.asdfad')(),error = function(e) {e})
<console>:17: 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.GeneratedMethodAccessor2.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.heart(ScalaServer.scala:512)
at org.ddahl.rscala.server.ScalaServer.run(ScalaServer.scala:539)
at org.ddahl.rscala.server.Main$.delayedEndpoint$org$ddahl$rscala$server$Main$1(Main.scala:5)
at org.ddahl.rscala.server.Main$delayedInit$body.apply(Main.scala:3)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:389)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
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.reflect.internal.util.ScalaClassLoader.$anonfun$run$2(ScalaClassLoader.scala:98)
at scala.reflect.internal.util.ScalaClassLoader.asContext(ScalaClassLoader.scala:32)
at scala.reflect.internal.util.ScalaClassLoader.asContext$(ScalaClassLoader.scala:30)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:129)
at scala.reflect.internal.util.ScalaClassLoader.run(ScalaClassLoader.scala:98)
at scala.reflect.internal.util.ScalaClassLoader.run$(ScalaClassLoader.scala:90)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:129)
at scala.tools.nsc.CommonRunner.run(ObjectRunner.scala:22)
at scala.tools.nsc.CommonRunner.run$(ObjectRunner.scala:21)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
at scala.tools.nsc.CommonRunner.runAndCatch(ObjectRunner.scala:29)
at scala.tools.nsc.CommonRunner.runAndCatch$(ObjectRunner.scala:28)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:61)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:88)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:99)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:104)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.RuntimeException
at $line322.$read$$iw$$iw$.$anonfun$res154$1(<console>:17)
... 39 more
java.lang.reflect.InvocationTargetException
java.lang.RuntimeException
<simpleError in (structure(function () { .rsI <- .rsWjWKOPE56 .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
>
>