说我有一个方法:
def foo(t: java.lang.reflect.Type) = ???
Run Code Online (Sandbox Code Playgroud)
我想打电话给它(Int, Int).
这样的电话怎么样?我试过了:
foo(typeOf[(Int, Int)])
Run Code Online (Sandbox Code Playgroud)
但它似乎没有工作,因为它返回scala.reflect.runtime.universe.Type而不是java.lang.reflect.Type.
我是Scala的新手,我遇到了currying的问题,无法理解下面代码的答案是144.希望你们能在这里帮助我.
谢谢
def product (f: Int => Int)(a: Int, b: Int) : Int =
if(a>b) 1
else f(a) * product(f)(a + 1, b)
product(x => x * x)(3, 4) //answer = 144
Run Code Online (Sandbox Code Playgroud) 大家好我对来自C#的Scala相当新.
我正在尝试编写自己的累积版本(折叠)我想知道为什么我遇到以下问题:
def accumulate[T](list : List[T], initial: T, f: (T, T) => T) : T = {
@tailrec def loop[T](list: List[T], accum: T) : T =
if(list.length == 0)
accum
else{
val head : T = list.head
val res : T = f(accum,head)
loop[T](list.tail, res)
}
loop(list,initial)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
type mismatch;
found : accum.type (with underlying type T)
required: T
val res : T = f(accum,head)
^
Run Code Online (Sandbox Code Playgroud)
考虑到一切都是T型,我无法看到我的类型不匹配.
任何想法/帮助将不胜感激.
布莱尔
所以我试图测试ping()方法,并确保它调用connect()和disconnect().我嘲笑了物体foo和物体Connection.然后我确实stub(mockFoo.connect()).toReturn(mockConn)希望那么我ping()应该回归真实的断言.然而,不知何故,这种存根不会经历.我怀疑可能在内部调用connect时,mockFoo实际上没有调用connect(),因此没有返回模拟Connection,但我不确定.
public class foo{
public boolean ping(){
bool ping = false;
Connection conn = null;
try{
conn = connect();
ping = true;
}catch(Exception e){
}finally{
disconnect(conn);
}
return ping;
}
public Connection connect(){
//some implementation
return connect
}
Run Code Online (Sandbox Code Playgroud) 我在Horstman书(第291-292页)中的示例代码中定义和使用案例类 作为简单Scala actor系统中的消息.
问题是在接收模式匹配中没有识别类,并且控制正在落入case _ =>语句.
代码如下所示.一切都适用于非案例类消息.
SENDER:在演员Rcoord中,行为方法是:
def act() {
alive(9000)
register('rcoord, self)
proc_reg.start // start the process register actor
loop {
try {
receive {
case 'process =>
process_counter += 1
process_number = process_counter
spawn_process(process_number, sprocs)
case class CreateTS(xxx: Int)
proc_reg ! CreateTS(process_number)
case 'stats =>
Console.println("received msg from client to view statistics")
//sender ! 'ok
case 'stop =>
Console.println("received msg that client is terminating")
//sender ! 'bye
} // end receive …Run Code Online (Sandbox Code Playgroud) 我有这个工作代码在String中的字符和包含索引的List之间创建Map.
scala> "Lollipop".zipWithIndex.foldLeft(Map[Char, List[Int]]())((acc, t) => acc + (t._1 -> (acc.getOrElse(t._1, List[Int]()) :+ t._2)))
res122: scala.collection.immutable.Map[Char,List[Int]] = Map(i -> List(4), L -> List(0), l -> List(2, 3), p -> List(5, 7), o -> List(1, 6))
Run Code Online (Sandbox Code Playgroud)
但使用acc.getOrElse看起来势在必行.是否有更多功能性方法可以隐藏用户?
val reslist = List(200.0,-100.00,50.80,-400.83, 800.003,-6.513214114672146E85, -1.2425461624057028E86, -4.7624471630469706E86, -3.6046499228286203E86, 0.0, -8.833653923554989E85, 0.000, -4.795843631190487E85, -5.34142100270833E86, -3.48087737474366E85, -2.811146396971388E86, -6.923235225460886E86, -6.513214114672146E85, 0.00000, -1.2425461624057028E86, -7.073704018243951E85, -9.633244016491059E86, -1.1418901590222212E86, -2.115257701350766E86, -1.1418901590222212E86, -3.48087737474366E85,-1.0676381955303372E86,500.56, 2.900556,400.56,-48956.00,4509.0005);
val weightlistzi = reslist.zipWithIndex
// List((200.0,0), (-100.0,1), (50.8,2), (-400.83,3), (800.003,4), (-6.513214114672146E85,5), (-1.2425461624057028E86,6), (-4.7624471630469706E86,7), (-3.6046499228286203E86,8), (0.0,9), (-8.833653923554989E85,10), (0.0,11), (-4.795843631190487E85,12), (-5.34142100270833E86,13), (-3.48087737474366E85,14), (-2.811146396971388E86,15), (-6.923235225460886E86,16), (-6.513214114672146E85,17), (0.0,18), (-1.2425461624057028E86,19), (-7.073704018243951E85,20), (-9.633244016491059E86,21), (-1.1418901590222212E86,22), (-2.115257701350766E86,23), (-1.1418901590222212E86,24), (-3.48087737474366E85,25), (-1.0676381955303372E86,26), (500.56,27), (2.900556,28), (400.56,29), (-48956.0,30), (4509.0005,31))
// I am sorting it here.
val resultlist = weightlistzi.sortWith { (x: (Double,Int), y: (Double,Int)) => x._1 …Run Code Online (Sandbox Code Playgroud)