简体   繁体   English

从列表创建地图的更好方法

[英]a better way to create a map from a list

I have a function that look like: 我有一个看起来像的功能:

 def functionTrans(f: FunctionDec) = {

    // some FunctionDec manipulation

    f.name -> FuncEntry(/* something here*/)
  }

Witch basically do a manipulation of FunctionDec and turn into a Pair (String, FuncEntry). 女巫基本上是对FunctionDec进行操作,然后变成对(字符串,FuncEntry)。 Also i have a Map[String, FuncEntry] called varsEnv and a List[FunctionDec] called decs . 另外我有一个地图[字符串,FuncEntry]所谓varsEnv列表[FunctionDec]所谓DECS。 I need apply that manipulation to all the list and return an augmented Map. 我需要将该操作应用于所有列表,并返回增强的Map。 What i did looks like: 我所做的看起来像:

val venvWithFunction = decs.foldLeft(varsEnv)(_ + functionTrans(_))

There's a better scala-colection way to do this? 有更好的scala-colection方法来做到这一点吗?

decs.map(functionTrans _).toMap应该足够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM