本文介绍了我可以使用for-comprehenion/yield在Scala中创建地图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以屈服"到地图上吗?
Can I "yield" into a Map?
我尝试过
val rndTrans = for (s1 <- 0 to nStates;
s2 <- 0 to nStates
if rnd.nextDouble() < trans_probability)
yield (s1 -> s2);
(并且使用,
而不是->
),但出现错误
(and with ,
instead of ->
) but I get the error
TestCaseGenerator.scala:42: error: type mismatch;
found : Seq.Projection[(Int, Int)]
required: Map[State,State]
new LTS(rndTrans, rndLabeling)
我明白了为什么,但是我看不出如何解决这个问题:-/
I can see why, but I can't see how to solve this :-/
推荐答案
scala> (for(i <- 0 to 10; j <- 0 to 10) yield (i -> j)) toMap
res1: scala.collection.immutable.Map[Int,Int] = Map((0,10), (5,10), (10,10), (1,10), (6,10), (9,10), (2,10), (7,10), (3,10), (8,10), (4,10))
这篇关于我可以使用for-comprehenion/yield在Scala中创建地图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!