Sie finden die Funktion Signatur f: (Map[String,List[(String, Double)]], String) => Double
und nicht nur den Rückgabetyp übergeben müssen. Ein trivialisiert Beispiel unten:
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
auf Ihrem Anwendungsfall abhängig, könnte es eine gute Idee, auch otherFunction
testMap
und name
als Parameter zu übergeben.
Dies gibt keine Funktion als Parameter weiter, sondern gibt das Ergebnis einer Funktion weiter. –
ja, du hast Recht – MLeiria