与Java Regex Thread Safe类似?,我想知道scala正则表达式的这种用法是否真的是线程安全的?多个线程是否能够在同一个对象M上调用m而不会在结果中相互干扰?
object R {
val pat = """a(\d)""".r
}
class M {
def m(s: String): Option[Int] = {
s match {
case R.pat(i) => Some(i.toInt)
case _ => None
}
}
}
Run Code Online (Sandbox Code Playgroud) 如何从与#&&等结合的外部进程中捕获异常?
scala> import scala.sys.process._
scala> try{ "throw " ! }catch{ case e: Exception => }
res1: AnyVal = ()
scala> try{ "throw " #&& "ls" ! }catch{ case e: Exception => }
Exception in thread "Thread-10" java.io.IOException: Cannot run program "throw": error=2, No such file or directory
Run Code Online (Sandbox Code Playgroud) 我想在每个面板的底部画一个框,每个面板可能有 不同的y 范围。该框应始终位于 x 轴上的 3 到 7 之间,并且从面板底部的边框到 y 轴高度的 5% 左右。所以我想指定 egannotation_custom或geom_rect单位的坐标,例如:
xmin=unit(3, "native"), xmax=unit(7,"native"), ymin=unit(0,"npc"), ymax=unit(0.05,"npc")
Run Code Online (Sandbox Code Playgroud)
我可以做到,但这些单位似乎被忽略了,它总是原生的。
library("grid")
library("ggplot2")
df <- data.frame(x=c(1:10,1:10),y=c(1:10,1001:1010),condition=rep(c("A","B"),each=10))
g <- rectGrob(gp=gpar(fill="black"))
p <- ggplot(df, aes(x=x,y=y)) + geom_line() + facet_wrap(~ condition, scales="free_y")
p + geom_rect(xmin=3,xmax=7,ymin=0.56,ymax=1,colour="black", fill="black")
g <- rectGrob(gp=gpar(fill="black"))
p + annotation_custom(g, xmin=3, xmax=7, ymin=0, ymax=1 )
p + annotation_custom(g, xmin=unit(3, "native"), xmax=unit(7,"native"),ymin=unit(0,"npc"),ymax=unit(1,"npc") )
Run Code Online (Sandbox Code Playgroud) 我使用limejs,我宣布一个函数,如:
function up(){
var pos = global.getPosition();
pos.x += 2;
global.setPosition(pos);
}
Run Code Online (Sandbox Code Playgroud)
并使用它来调用它
lime.scheduleManager.schedule(up,global);
Run Code Online (Sandbox Code Playgroud)
如何摆脱全局变量?
> writeBin(1:3, raw(), size=4, endian="little")
[1] 01 00 00 00 02 00 00 00 03 00 00 00
> writeBin(c(1,2,3), raw(), size=4, endian="little")
[1] 00 00 80 3f 00 00 00 40 00 00 40 40
> writeBin(c(1:3), raw(), size=4, endian="big")
[1] 00 00 00 01 00 00 00 02 00 00 00 03
> writeBin(c(1,2,3), raw(), size=4, endian="big")
[1] 3f 80 00 00 40 00 00 00 40 40 00 00
Run Code Online (Sandbox Code Playgroud)
第一个和第三个结果是我所期望的,但为什么我得到一个指定为c(1,2,3)的向量的不同原始向量?