只是找不到将Hex字符串转换为Scala中的数字(Int,Long,Short)的方法.
有类似的东西"A".toInt(base)吗?
我正在努力弄清楚如何在scala中存储或传递类型.
我想要实现的是这样的:
abstract class Foo( val theType : type )
object Foo{
case object Foo1 extends Foo(String)
case object Foo2 extends Foo(Long)
}
Run Code Online (Sandbox Code Playgroud)
所以在某些时候我可以这样做:
theFoo match{
case String => "Is a string"
case Long => "Is a long"
}
Run Code Online (Sandbox Code Playgroud)
当获得能够施放它的物体时:
theFoo.asInstanceOf[Foo1.theType]
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果可能,是一个很好的方法吗?我最终想要实现的是为字节流处理编写伪模式.例如,如果我有一个模式,Array(Foo1,Foo1,Foo2,Foo3,Foo1)我可以解析抱怨该模式的字节数组,如果在某些时候我有一个不同的字节流,我可以编写一个新的模式,Array(Foo3, Foo4, Foo5)而不必重新实现解析逻辑.
问候,
按要求编辑
假设我有一个名为Command1的数组[Byte] = A973928CB3883FB123
此字节中的数据固定在位置和长度上.换句话说,我知道1-4位是例如一个小日期,5-9是客户的名字等等.
我想要的是编写一个单独的解析函数,它只作为参数的一个模式,并返回模式中每个参数的实际值.
trait Command{
//This is implemented in every command
val schema : List[Tuple[String,Int,Int,Type]] //Position,Size,DataType
def parse() : List[Tuple[String,Int,Int,Type,Any]] = schema.map(//match using the type)
}
class Command1 …Run Code Online (Sandbox Code Playgroud) 我正在测试libgdx,但我陷入了用户输入处理.
我的第一次尝试是直接从渲染方法使用Gdx.input,但我觉得重新发明轮子,因为当我得到触摸事件时,我正在编写大量代码来检测输入区域.
我几乎肯定应该使用Actor类更好的方法,但是因为事件永远不会发生,所以我必须做错事.
这是我的代码:
...
Texture texture = new Texture(Gdx.files.internal("assets/sprite-sheet.png"));
singlePlayerButton = new Image("SinglePlayerButton", new TextureRegion(texture,0,0,50,50)){
@Override
public boolean touchDown(float x, float y, int pointer) {
// TODO Auto-generated method stub
System.out.println("touch down");
return super.touchDown(x, y, pointer);
}
@Override
public void touchUp(float x, float y, int pointer) {
System.out.println("touch up");
}
};
stage.addActor(singlePlayerButton);
...
public void render(float delta) {
// Clear the screen
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.draw();
spriteBatch.end();
}
Run Code Online (Sandbox Code Playgroud)
图像显示效果不错,但无论我点击多少次都没关系,我从未获得过该事件.我错过了什么?注册活动?我在Stage或Actor类中找不到任何addTouchListener()方法.
谢谢!
您可以使用以下方法在Scala中创建新列表:
1 :: 2 :: Nil
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这可以改写为:
Nil.::(2.::(1))
Run Code Online (Sandbox Code Playgroud)
主要是因为:: fixty但是如果我写:
Nil :: 1 :: 2
Run Code Online (Sandbox Code Playgroud)
我得到"值::不是Int的成员"什么是完全预期的,因为在scaladoc Int中没有::,但我无法理解为什么如果我将其转换为:
1.::(2.::(Nil))
Run Code Online (Sandbox Code Playgroud)
它可以作为输出:
List(1.0, 2.0)
Run Code Online (Sandbox Code Playgroud)
它看起来像scalac自动强制转换为1与2Int不同的类型.那是对的吗?如果是,为什么会发生这种奇怪的类型?
有人可以帮助我掌握允许在scala中修改AST的转换对象吗?
网络上有很多例子,但我很难理解递归是如何发生的.
例如,如果我有这个AST:
def foo = {
true
}
Run Code Online (Sandbox Code Playgroud)
要么
def foo = true
Run Code Online (Sandbox Code Playgroud)
并希望将其转化为
def foo = {
println("foo")
true
}
Run Code Online (Sandbox Code Playgroud)
转换函数应该如何显示.在这一点上,我有这样的事情:
override def transform(tree: Tree) = tree match {
case defdef @ DefDef(_,_,_,_,_,rhs) => rhs match{
case b: Block => treeCopy.Block(b, createPrintln :: b.stats, b.expr)
case _ => //Manage functions without block
}
case t => super.transform(t)
}
private def createPrintln = Apply(Select(Ident("System.out"), newTermName("println")), List(Literal(Constant("foo"))))
Run Code Online (Sandbox Code Playgroud)
但是不起作用,说实话我只是应用我在示例中看到的但却无法弄清楚树是如何构建的."傻瓜"的解释将不胜感激.
[编辑]
Senia的例子很不错,但是当我有一个更复杂的树时,我仍然感到困惑:
imports bla.bla
class MyObject{
val x = 0
def foo …Run Code Online (Sandbox Code Playgroud) 我面临着方法性能极慢的问题:
HttpRequestDecoder.unfoldAndFireMessageReceived()
Run Code Online (Sandbox Code Playgroud)
和
Future$PromiseCompletingRunnable.run()
Run Code Online (Sandbox Code Playgroud)
这两种方法大约使用服务器中每个事务一半的时间。它发生在低吞吐量和高使用时间期间。
例如,凌晨 1 点,只有我向应用程序发出请求,我在 new relic 中得到如下图表:

在这个事务中,只有这两个方法消耗了 1 整秒,通过 hibernate 访问数据库甚至更慢!再次强调,应用程序中只有一个用户。
如果交易量较大,则需要更多时间:
在本例中,这两种方法平均耗时 2.5 秒,而我自己的代码耗时 1.5 秒,总共耗时 4 秒。我当时想,也许这只是新遗迹度量的误导。也许 newrelic 显示了这个方法的名称,但它实际上是我编写的代码。所以我决定获得一个像这样的自定义指标:
playController(){
//Start timer
//do the job
//stop the timer() and send metric to new relic
//return;
}
Run Code Online (Sandbox Code Playgroud)
结果是我的代码花了 1.5 秒。所以真正消耗这个时间的是播放请求处理程序。
当负载较高时,这种行为会杀死我的应用程序。当吞吐量约为每分钟 500 个请求时(并不是真正的高吞吐量!),这两种方法可能会消耗长达 20 秒的时间,但我的代码保持稳定在最大 3 秒。
我真的不认为这是一个线程问题,因为它甚至在只有一个用户时也会发生,但当有许多并发请求时,它就会变得非常有问题。我尝试更改“同步应用程序”的线程数(如文档中提到的),但我没有得到任何性能变化,甚至变得更糟。
我真的很担心这个问题,因为在 play 的邮件列表中也有类似的案例,已经两年多了,却没有任何答复!:
StackOverflow 中甚至有一个类似的问题,但对于 play 2.1 没有答案,也没有明显的活动:
以 Play Framework 作为后端的 NewRelic 中事务处理缓慢
有什么想法可能导致这种行为吗?
在mongo-go-driver中检查接口Cursor:
https://github.com/mongodb/mongo-go-driver/blob/master/mongo/cursor.go#L37
没有Limit或Skip功能。
如何分页结果?
我想尝试Sort或尝试时会遇到同样的问题Count。
有办法吗?或者,这是否还没有在官方驱动程序中实现?
scala ×4
java ×2
android ×1
cons ×1
game-engine ×1
libgdx ×1
mongo-go ×1
netty ×1
performance ×1
recursion ×1
reflection ×1