[英]How can I convert scala Map to a partial function
Currently I use the following snippet of code: 目前我使用以下代码片段:
private val aMap = Map(
"J" -> Journey,
"T" -> Training
)
def partialFunction = {
case x if aMap isDefinedAt x => aMap(x)
}
It seems to me that Maps naturally should define a partial function. 在我看来,地图自然应该定义一个部分功能。 Does Scala have any standard/more concise and explicit way to convert a map to a partial function? Scala是否有任何标准/更简洁明确的方法将地图转换为部分功能? Maybe some kind of an implicit conversion? 也许某种隐式转换?
A Scala Map
actually is a PartialFunction
; Scala Map
实际上是 PartialFunction
; you can just use it as such. 你可以这样使用它。 No conversion required, implicit or explicit. 无需转换,隐式或显式。
For instance: 例如:
val pf: PartialFunction[Int, String] = Map(1 -> "one")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.