加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为什么Scala中没有mapKeys?

发布时间:2020-12-16 18:31:50 所属栏目:安全 来源:网络整理
导读:Scala Collection库有mapValues和filterKeys.它没有mapKeys的原因可能是性能方面(关于HashMap实现),正如这里讨论的Haskell: Why there’s not mapKeys in Data.Hashmap? 然而. 除了性能影响之外,我发现自己至少需要mapKeys和mapValues一样多,只是为了按摩数
Scala Collection库有mapValues和filterKeys.它没有mapKeys的原因可能是性能方面(关于HashMap实现),正如这里讨论的Haskell: Why there’s not mapKeys in Data.Hashmap?

然而.

除了性能影响之外,我发现自己至少需要mapKeys和mapValues一样多,只是为了按摩数据(即我使用地图进行数据抽象,而不是使用其获取速度).

我错了,您会使用哪种数据模型?元组?

解决方法

不知道为什么它不在标准库中,但是您可以使用隐式类轻松地对您的库进行pimp

implicit class MapFunctions[A,B](val map: Map[A,B]) extends AnyVal {
    def mapKeys[A1](f: A => A1): Map[A1,B] = map.map({ case (a,b) => (f(a),b) })
  }

  val m = Map(1 -> "aaa",2 -> "bbb")

  println(m.mapKeys(_ + 1))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读