Scala: How to create a Map[K,V] from a Set[K] and a function from K to V?

前端 未结 7 1211
栀梦
栀梦 2020-12-15 07:37

What is the best way to create a Map[K,V] from a Set[K] and function from K to V?

For example, suppose I have

7条回答
  •  情话喂你
    2020-12-15 08:14

    The other solutions lack creativity. Here's my own version, though I'd really like to get rid of the _.head map.

    s groupBy identity mapValues (_.head) mapValues func
    

提交回复
热议问题