2010-11-18 87 views
16

我可以「屈服」成地圖嗎?我可以使用for-comprehenion/yield在Scala中創建地圖嗎?

我已經試過

val rndTrans = for (s1 <- 0 to nStates; 
        s2 <- 0 to nStates 
         if rnd.nextDouble() < trans_probability) 
          yield (s1 -> s2); 

(與,代替->),但我得到的錯誤

TestCaseGenerator.scala:42: error: type mismatch; 
found : Seq.Projection[(Int, Int)] 
required: Map[State,State] 
    new LTS(rndTrans, rndLabeling) 

我明白爲什麼,但我看不出如何解決這個: -/

回答

15
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)) 
+0

嗯,大概接近我正在尋找,但我得到:'錯誤:值toMap不是Seq.Projection的成員[(Int,Int)]' – aioobe 2010-11-18 11:03:11

+0

這很奇怪。你正在使用哪個版本的Scala? – aioobe 2010-11-18 11:04:51

+0

我正在使用2.8.0.final – 2010-11-18 11:06:39

4

替代(作品2.7):

scala> Map((for(i <- 0 to 10; j <- 0 to 10) yield (i -> j)): _*) 
res0: 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)) 
+0

太好了。我現在堅持2.7,所以我會用這個。 – aioobe 2010-11-18 11:19:47

12

斯卡拉2.8另一種解決方案:

Welcome to Scala version 2.8.1.r23457-b20101106033551 (Java HotSpot(TM) Client VM, Java 1.6.0_22). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> import scala.collection.breakOut    
import scala.collection.breakOut 

scala> val list: List[(Int,Int)] = (for(i<-0 to 3;j<-0 to 2) yield(i->j))(breakOut) 
list: List[(Int, Int)] = List((0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2), (3,0), (3,1), (3,2)) 

scala> val map: Map[Int,Int] = (for(i<-0 to 3;j<-0 to 2) yield(i->j))(breakOut)  
map: Map[Int,Int] = Map((0,2), (1,2), (2,2), (3,2)) 

scala> val set: Set[(Int,Int)] = (for(i<-0 to 3;j<-0 to 2) yield(i->j))(breakOut) 
set: Set[(Int, Int)] = Set((2,2), (3,2), (0,1), (1,2), (0,0), (2,0), (3,1), (0,2), (1,1), (2,1), (1,0), (3,0)) 

scala> 
+7

+1永遠神祕(但有用!)breakOut。如果我錯了,請糾正我的錯誤,但我認爲它的性能略好,因爲它提供了一個允許直接生成地圖的構建器(而不是構建另一個後來轉換的集合)。 – 2010-11-18 21:01:48

+2

@Zwirb你是對的。還有+1,因爲我不知道你可以將它用於/ yield語法! :-) – 2010-11-18 23:15:38

1
val rndTrans = (
    for { 
    s1 <- 0 to nStates 
    s2 <- 0 to nStates if rnd.nextDouble() < trans_probability 
    } yield s1 -> s2 
) (collection.breakOut[Any, (Int, Int), Map[Int, Int]]) 
相關問題