我可以使用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))