Function 调试围绕lambda体、匿名类实例等传递的scala代码的最佳技术?
您发现了什么最好的技术来调试在lambda体、匿名类实例和诸如此类的东西之间传递的scala代码 我相信,一旦我学得更好,Scala将使我的工作效率更高,但当我试图弄清楚是什么生成了回调(或lambda体等),我在程序执行流程的特定点调用回调时,有时会感到困惑。我尝试了一种技术,你可以在下面看到它不起作用。。。我提供了这个代码示例,以便您可以看到我试图实现的目标Function 调试围绕lambda体、匿名类实例等传递的scala代码的最佳技术?,function,scala,debugging,lambda,Function,Scala,Debugging,Lambda,您发现了什么最好的技术来调试在lambda体、匿名类实例和诸如此类的东西之间传递的scala代码 我相信,一旦我学得更好,Scala将使我的工作效率更高,但当我试图弄清楚是什么生成了回调(或lambda体等),我在程序执行流程的特定点调用回调时,有时会感到困惑。我尝试了一种技术,你可以在下面看到它不起作用。。。我提供了这个代码示例,以便您可以看到我试图实现的目标 def fork[A](a: => Par[A]): Par[A] = { val marker = new java.u
def fork[A](a: => Par[A]): Par[A] = {
val marker = new java.util.Date().getTime
log("about to return future with marker: " + marker)
val func = (es: ExecutorService) => new Future[A] {
def apply(cb: A => Unit): Unit = {
log("applying callback (w/ eval) in future apply method: " + this)
eval(es)(a(es)(cb))
}
override def toString : String = { "execSvc -> future w/marker: " + marker}
}
log("returning future creation func: " + func.toString())
func
}
// a log method that uses a simmple timestamp to help order print statements emitted in multi-threaded programs,
// and which deliberately slows things down so we don't see the same time stamp repeated. cheesy, but
// seems to serve its purpose
def log(msg: String): Unit = {
println(new java.util.Date().getTime() + " thread: " + Thread.currentThread().getName + " " + msg)
for (i <- 1 to 100000000) {} // slow things down !
System.out.flush()
}
def fork[A](A:=>Par[A]):Par[A]={
val marker=new java.util.Date().getTime
日志(“即将返回带有标记的未来:“+marker”)
val func=(es:ExecutorService)=>新未来[A]{
def应用(cb:A=>单位):单位={
日志(“在将来的应用方法中应用回调(w/eval):+this)
评估(评估)(a(评估)(cb))
}
重写def toString:String={“execSvc->future w/marker:“+marker}
}
日志(“返回未来创建函数:+func.toString())
func
}
//一种日志方法,它使用simple时间戳帮助对多线程程序中发出的打印语句进行排序,
//故意放慢速度,这样我们就不会看到同样的时间戳重复出现。俗气,但是
//似乎达到了目的
def日志(消息:字符串):单位={
println(新java.util.Date().getTime()+“线程:”+thread.currentThread().getName+“”+msg)
因为(我快乐时光就在这里。给了我答案。下面的代码显示了我是如何将它应用到我的情况中的
def fork[A](a: => Par[A]): Par[A] = {
val marker = new java.util.Date().getTime
log("about to return future with marker: " + marker)
System.out.flush()
val func = new Function1[ExecutorService, Future[A]] {
def apply(es: ExecutorService) = {
new Future[A] {
def apply(cb: A => Unit): Unit = {
log("applying callback (w/ eval) in future apply method: " + this)
eval(es)(a(es)(cb))
}
override def toString: String = {
"I am a future object constructed from marker: " + marker
}
}
}
override def toString: String = {
"I am a method that takes an exec svc and returns a futture i was constructed from marker: " + marker
}
}
log("returning future creation func: " + func)
func
}
现在,我的输出包含了一些标记,我发现这些标记非常有助于追溯到我在某个点上实例化调用的功能块的位置:
1438988219429 thread: main applying callback (w/ eval) in future apply method: I am a future object constructed from marker: 1438988219269
1438988219460 thread: main in eval now / creating callable with marker: 1438988219460
1438988219491 thread: main submitting callable:fpinscala.parallelism.Nonblocking$Par$$anon$6@3fb6a447
1438988219526 thread: pool-1-thread-1 executing callable with 1438988219460
1438988219561 thread: pool-1-thread-1 about to return future with marker: 1438988219561
1438988219598 thread: pool-1-thread-1 returning future creation func: I am a method that takes an exec svc and returns a futture i was constructed from marker: 1438988219561
1438988219651 thread: pool-1-thread-1 applying callback (w/ eval) in future apply method: I am a future object constructed from marker: 1438988219561
1438988219686 thread: pool-1-thread-1 in eval now / creating callable with marker: 1438988219686
Woops..可能问得太早了..答案可能在这里:我会尝试一下,如果它真的起作用,就结束这个问题。旁注:当然,生成的代码比没有FunctionN签名的代码要冗长得多…我会在调试后清理它..但是现在输出非常方便。
1438988219429 thread: main applying callback (w/ eval) in future apply method: I am a future object constructed from marker: 1438988219269
1438988219460 thread: main in eval now / creating callable with marker: 1438988219460
1438988219491 thread: main submitting callable:fpinscala.parallelism.Nonblocking$Par$$anon$6@3fb6a447
1438988219526 thread: pool-1-thread-1 executing callable with 1438988219460
1438988219561 thread: pool-1-thread-1 about to return future with marker: 1438988219561
1438988219598 thread: pool-1-thread-1 returning future creation func: I am a method that takes an exec svc and returns a futture i was constructed from marker: 1438988219561
1438988219651 thread: pool-1-thread-1 applying callback (w/ eval) in future apply method: I am a future object constructed from marker: 1438988219561
1438988219686 thread: pool-1-thread-1 in eval now / creating callable with marker: 1438988219686