[英]Scala - return a function from a map
In scala, how would i declare & instantiate a map that returns a function (for the sake of argument? A function that accepts two variables, one a String, one an Int)? 在Scala中,我将如何声明并实例化返回一个函数的映射(出于参数考虑?一个接受两个变量的函数,一个是String,一个是Int)?
I am envisioning: 我正在设想:
val myMap = Map[String, (String,Int)=>Boolean](
WHAT GOES HERE???
)
Let's just map the string "a" to this cool function. 让我们将字符串“ a”映射到这个很酷的函数。 I don't care much about what the function does - returns true, perhaps?
我不太在意函数的功能-返回true,也许吗?
Try this: 尝试这个:
val myMap = Map[String, (String, Int) => Boolean](
"Test" -> ((s, i) => true)
)
you can do something like this: 您可以执行以下操作:
val map = Map("key" -> { (str: String, n: Int) =>
str.indexOf(n) == -1
})
result: 结果:
> map: scala.collection.immutable.Map[String,(String, Int) => Boolean] = Map(key - <function2>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.