2016-07-22 50 views
0

我嘗試用於理解,以製作一個字符串映射到MyData案例類。爲理解產量地圖

這是我曾嘗試:

case class MyDataProperty(name: String, value: String) 
case class MyData(props: List[MyDataProperty]) 


def makeMyData(configs: List[Config]): Map[String, MyData] = { 
    for { 
     // loop through all configurations 
     conf <- configs 
     // Retrieve each config's list of properties and make a list of MyDataProperty object from it 
     props <- for(prop <- conf.properties) yield (MyDataProperty(prop.name, prop.value)) 
    } yield (conf.name -> MyData(props)) toMap 
} 

此代碼給我多編譯錯誤。建立這種理解和產生地圖的巢穴的正確方法是什麼?

+0

什麼是你所得到的編譯錯誤? – tuxdna

回答

0

隨着for comprehensions你的代碼應該看起來像以下:

def makeMyData(configs: List[Config]): Map[String, MyData] = 
    (for { 
    conf <- configs 
    props = for (prop <- conf.properties) yield MyDataProperty(prop.name, prop.value) 
    } yield conf.name -> MyData(props)).toMap