堆栈机模拟器 Scala
Stack Machine Emulator Scala
这里我有一个最终函数,它应该在指令列表上执行 foldLeft。我得到
type mismatch;
found : (List[Double], scala.collection.immutable.Map[String,Double])
required: Map[String,Double]
instructionList.foldLeft(Nil : List[Double], Map.empty[String,Double])((acc:(List[Double], Map[String,Double]), SMI:StackMachineInstruction) => {
我不确定我是否正确初始化了累加器
def emulateStackMachine(instructionList: List[StackMachineInstruction]): Map[String, Double] =
{
instructionList.foldLeft((Nil : List[Double], Map.empty[String,Double]))((acc:(List[Double], Map[String,Double]), SMI:StackMachineInstruction) => {
emulateSingleInstruction(acc._1, acc._2, SMI)
})
}
您不是在创建元组,而是像传递两个参数调用一样传递值。使用:
((Nil : List[Double], Map.empty[String,Double])) // double parens
或
(List.empty[Double] -> Map.empty[String,Double]) // -> syntax
创建元组并将其传递到调用中。
另外你必须改变你的输出类型 - 它是
Map[String, Double]
而函数返回的值为:
(List[Double], Map[String,Double])
def emulateStackMachine(instructionList: List[StackMachineInstruction]): (List[Double], Map[String, Double]) = {
instructionList.foldLeft(List.empty[Double] -> Map.empty[String,Double])((acc, SMI) => {
emulateSingleInstruction(acc._1, acc._2, SMI)
})
}
// or
def emulateStackMachine(instructionList: List[StackMachineInstruction]): Map[String, Double] = {
instructionList.foldLeft(List.empty[Double] -> Map.empty[String,Double])((acc, SMI) => {
emulateSingleInstruction(acc._1, acc._2, SMI)
})._2
}
这里我有一个最终函数,它应该在指令列表上执行 foldLeft。我得到
type mismatch;
found : (List[Double], scala.collection.immutable.Map[String,Double])
required: Map[String,Double]
instructionList.foldLeft(Nil : List[Double], Map.empty[String,Double])((acc:(List[Double], Map[String,Double]), SMI:StackMachineInstruction) => {
我不确定我是否正确初始化了累加器
def emulateStackMachine(instructionList: List[StackMachineInstruction]): Map[String, Double] =
{
instructionList.foldLeft((Nil : List[Double], Map.empty[String,Double]))((acc:(List[Double], Map[String,Double]), SMI:StackMachineInstruction) => {
emulateSingleInstruction(acc._1, acc._2, SMI)
})
}
您不是在创建元组,而是像传递两个参数调用一样传递值。使用:
((Nil : List[Double], Map.empty[String,Double])) // double parens
或
(List.empty[Double] -> Map.empty[String,Double]) // -> syntax
创建元组并将其传递到调用中。
另外你必须改变你的输出类型 - 它是
Map[String, Double]
而函数返回的值为:
(List[Double], Map[String,Double])
def emulateStackMachine(instructionList: List[StackMachineInstruction]): (List[Double], Map[String, Double]) = {
instructionList.foldLeft(List.empty[Double] -> Map.empty[String,Double])((acc, SMI) => {
emulateSingleInstruction(acc._1, acc._2, SMI)
})
}
// or
def emulateStackMachine(instructionList: List[StackMachineInstruction]): Map[String, Double] = {
instructionList.foldLeft(List.empty[Double] -> Map.empty[String,Double])((acc, SMI) => {
emulateSingleInstruction(acc._1, acc._2, SMI)
})._2
}