是否可以在scala中声明变量作为一系列指令的结果?我正在尝试做一些与以下F#片段相同的东西
{{1}}
答案 0 :(得分:2)
当然,你可以在Scala中做这样的事情:
val (gSmall, gLarge, defaultOrder) =
if (g1remainingNodesToMap.size <= g2remainingNodesToMap.size) {
(g1remainingNodesToMap, g2remainingNodesToMap, true)
} else {
(g2remainingNodesToMap, g1remainingNodesToMap, false)
}
答案 1 :(得分:2)
Scala中的等效内容如下:
val (gSmall, gLarge, defaultOrder) =
if (g1remainingNodesToMap.size <= g2remainingNodesToMap.size)
(g1remainingNodesToMap, g2remainingNodesToMap, true)
else (g2remainingNodesToMap, g1remainingNodesToMap, false)
现在,您可以引用gSmall
,gLarge
和defaultOrder
。为了使这个具体:
scala> val g1remainingNodesToMap = Map("one" -> 1, "two" -> 2)
g1remainingNodesToMap: scala.collection.immutable.Map[String,Int] = Map(one -> 1, two -> 2)
scala> val g2remainingNodesToMap = Map("three" -> 3, "four" -> 4, "five" -> 5)
g2remainingNodesToMap: scala.collection.immutable.Map[String,Int] = Map(three -> 3, four -> 4, five -> 5)
scala> :paste
// Entering paste mode (ctrl-D to finish)
val (gSmall, gLarge, defaultOrder) =
if (g1remainingNodesToMap.size <= g2remainingNodesToMap.size)
(g1remainingNodesToMap, g2remainingNodesToMap, true)
else (g2remainingNodesToMap, g1remainingNodesToMap, false)
// Exiting paste mode, now interpreting.
gSmall: scala.collection.immutable.Map[String,Int] = Map(one -> 1, two -> 2)
gLarge: scala.collection.immutable.Map[String,Int] = Map(three -> 3, four -> 4, five -> 5)
defaultOrder: Boolean = true
scala> gSmall
res0: scala.collection.immutable.Map[String,Int] = Map(one -> 1, two -> 2)
scala> gLarge
res1: scala.collection.immutable.Map[String,Int] = Map(three -> 3, four -> 4, five -> 5)
scala> defaultOrder
res2: Boolean = true