scala 2.8 集合不一致?

发布于 2024-09-18 15:31:10 字数 225 浏览 4 评论 0 原文

为什么方法transformmap的就地突变版本)和retainfilter的就地突变版本>) 仅在 mutable.Map 上定义,但不在 mutable.Buffermutable.Set 上定义?所有可变集合不都应该支持这些方法吗?

why the methods transform (in-place mutation version of map) and retain (in-place mutation version of filter) are defined on only mutable.Map but not on mutable.Buffer and mutable.Set? shouldnt all mutable collections support these methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2024-09-25 15:31:10

Map关于键和值的概念,这些概念不属于 设置缓冲区

Map 是由键和值对(也称为映射或关联)组成的 Iterable。
地图上的基本操作与集合上的基本操作类似。

但您的操作列在:

可变映射此外还支持下表中总结的操作。

ms transform f

使用函数f 转换映射ms 中的所有关联值。

ms retain p

仅保留 ms 中具有满足谓词 pkey 的映射。


一零零一评论:

retaintransform 本质上分别是 filtermap 的就地突变版本,并且可以很容易地定义在 SetBuffer 上。
我不明白它们是如何特定于地图的。

我认为 retaintransform 为诸如 地图过滤器来自TraversableLike

SetBuffer 中实现 transform 不会增加任何价值,因为它只是与 map 不同。


注意:Michael Kebe 评论:

还有一件事。 immutable.MapLiketransform 方法,但没有 retain 方法。


(与 mutable.MapLike 两者都有)

但这似乎符合 转换操作 如果转换通过过滤和转换现有地图的绑定生成新地图

这是 转换源代码

def transform[C, That](f: (A, B) => C)(implicit bf: CanBuildFrom[This, (A, C), That]): That = {
  val b = bf(repr)
  for ((key, value) <- this) b += ((key, f(key, value)))
  b.result
}

retain的源代码,却修改了当前实例,只能兼容Mutable对象:

/** Retains only those mappings for which the predicate
 *  `p` returns `true`.
 *
 * @param p  The test predicate 
 */
def retain(p: (A, B) => Boolean): this.type = {
  for ((k, v) <- this ; if !p(k, v))
    this -= k

  this
}

The operations retain and transform of Map are about keys and values, concepts which are not part of a Set or a Buffer.

A Map is an Iterable consisting of pairs of keys and values (also named mappings or associations).
The fundamental operations on maps are similar to those on sets.

But your operations are listed in:

Mutable maps support in addition the operations summarized in the following table.

ms transform f

Transforms all associated values in map ms with function f.

ms retain p

Keeps only those mappings in ms that have a key satisfying predicate p.


one-zero-zero-one comments:

retain and transform are essentially in-place mutation versions of filter and map respectively and can be easily defined on Set and Buffer.
I don't see how they are Map-specific.

I would argue that retain and transform offer Map-specific implementations (in that their implementation specifically deals with keys and values) for features like the ones provided by map and filter from TraversableLike.

Implementing transform in Set and Buffer would add little value as it would simply deffer to map.


Note: Michael Kebe comments:

One more thing. immutable.MapLike has the method transform, but not the method retain.

(as opposed to mutable.MapLike which has both)

This seems however in-line with the nature of transformation operations if transform produces a new map by filtering and transforming bindings of an existing map.

Here is the source code for transform

def transform[C, That](f: (A, B) => C)(implicit bf: CanBuildFrom[This, (A, C), That]): That = {
  val b = bf(repr)
  for ((key, value) <- this) b += ((key, f(key, value)))
  b.result
}

Source code for retain, however, modified the current instance, which can only be compatible with Mutable objects:

/** Retains only those mappings for which the predicate
 *  `p` returns `true`.
 *
 * @param p  The test predicate 
 */
def retain(p: (A, B) => Boolean): this.type = {
  for ((k, v) <- this ; if !p(k, v))
    this -= k

  this
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文