Scala将函数作为参数传递给另一个函数
抱歉弄得一团糟。我在斯卡拉还是有办法的。将所有问题重新表述如下:Scala将函数作为参数传递给另一个函数,scala,function-parameter,Scala,Function Parameter,抱歉弄得一团糟。我在斯卡拉还是有办法的。将所有问题重新表述如下: def func1(x: Double, y: Double) = { x+y } def func2(x: Double, y: Double) = { x-y } def calc(f: (Double, Double) => Double, z: Int) = { f(1,2) + z } //Sometimes I want to call calc(func1(1,2), 3) //Other
def func1(x: Double, y: Double) = {
x+y
}
def func2(x: Double, y: Double) = {
x-y
}
def calc(f: (Double, Double) => Double, z: Int) = {
f(1,2) + z
}
//Sometimes I want to call
calc(func1(1,2), 3)
//Other times
calc(func2(1,2), 3)
我得到了这个错误:
<console>:52: error: type mismatch;
found : Double
required: (Double, Double) => Double
calc(func1(1,2), 3)
^
什么是调用calc的正确方法
谢谢首先,你正在使用一个变量。不想详细阅读更多信息,这被认为是一个不好的做法,如果你的映射打算在类中本地使用,你可以创建一个可变映射 回到你的问题: 如果希望函数按预期工作,那么应该确保doSomeComputing函数返回otherFunction所期望的值作为输入参数,如下所示
def doSomeComputation(m1: Map[String, List[(String, Double)]], name: String) = {
(Map("some_key" -> List(("tuple1",1.0))), "tuple2")
}
返回类型为Map[String,List[String,Int]],String
然而,这对你想做什么没有多大意义,如果你能清楚地提到你想实现什么,我可以帮助你理解。注意,f的参数,即1和2,在calc的主体中提供 因此,您不需要为传入的函数指定任何参数
calc(func1, 3)
calc(func2, 3)
您需要传递函数签名f:Map[String,List[String,Double]],String=>Double,而不仅仅是返回类型。下面是一个简单的例子:
var testMap: Map[String, List[(String, Double)]] = Map(
"First" -> List(("a", 1.0), ("b", 2.0)),
"Second" -> List(("c", 3.0), ("d", 4.0))
)
// testMap: Map[String,List[(String, Double)]] = Map(First -> List((a,1.0), (b,2.0)), Second -> List((c,3.0), (d,4.0)))
def doSomeComputation(m1: Map[String, List[(String, Double)]], name: String): Double = {
m1.getOrElse(name, List[(String, Double)]()).map( x => x._2 ).max
}
// doSomeComputation: (m1: Map[String,List[(String, Double)]], name: String)Double
def doSomeOtherComputation(m1: Map[String, List[(String, Double)]], name: String): Double = {
m1.getOrElse(name, List[(String, Double)]()).map( x => x._2 ).min
}
// doSomeOtherComputation: (m1: Map[String,List[(String, Double)]], name: String)Double
def otherFunction(f: (Map[String, List[(String, Double)]], String) => Double, otherName: String) = {
f(testMap, "First") * otherName.length
}
// otherFunction: (f: (Map[String,List[(String, Double)]], String) => Double, otherName: String)Double
println(otherFunction(doSomeComputation, "computeOne"))
// 20.0
println(otherFunction(doSomeOtherComputation, "computeOne"))
// 10.0
根据您的用例,最好将testMap和name作为参数传递给其他函数。这不是将函数作为参数传递,而是传递函数的结果。是的,您是对的
var testMap: Map[String, List[(String, Double)]] = Map(
"First" -> List(("a", 1.0), ("b", 2.0)),
"Second" -> List(("c", 3.0), ("d", 4.0))
)
// testMap: Map[String,List[(String, Double)]] = Map(First -> List((a,1.0), (b,2.0)), Second -> List((c,3.0), (d,4.0)))
def doSomeComputation(m1: Map[String, List[(String, Double)]], name: String): Double = {
m1.getOrElse(name, List[(String, Double)]()).map( x => x._2 ).max
}
// doSomeComputation: (m1: Map[String,List[(String, Double)]], name: String)Double
def doSomeOtherComputation(m1: Map[String, List[(String, Double)]], name: String): Double = {
m1.getOrElse(name, List[(String, Double)]()).map( x => x._2 ).min
}
// doSomeOtherComputation: (m1: Map[String,List[(String, Double)]], name: String)Double
def otherFunction(f: (Map[String, List[(String, Double)]], String) => Double, otherName: String) = {
f(testMap, "First") * otherName.length
}
// otherFunction: (f: (Map[String,List[(String, Double)]], String) => Double, otherName: String)Double
println(otherFunction(doSomeComputation, "computeOne"))
// 20.0
println(otherFunction(doSomeOtherComputation, "computeOne"))
// 10.0