为什么 Seq[V] 不扩展 Map[Int,V],Set[V] 也不扩展 Map[V,Bool]?

发布于 2024-10-10 08:33:32 字数 289 浏览 4 评论 0原文

Iterable 的三个直接子类型是 MapSeqSet。除了性能问题之外,Seq 似乎是从整数到值的映射,而 Set 是从值到布尔值的映射(如果值在集,否则为 false)。

如果是这种情况,为什么不通过使 Seq[V] 扩展 Map[Int, V]Set[V] 在类型系统中表达这一点 扩展Map[V, Boolean]

The three immediate subtypes of Iterable are Map, Seq, and Set. It seems like—aside from performance issues—a Seq is a map from integers to values, and a Set is a map from values to booleans (true if the value is in the set, false otherwise).

If this is the case, why is this not expressed in the type system by making Seq[V] extend Map[Int, V] and Set[V] extend Map[V, Boolean]?

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

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

发布评论

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

评论(3

咆哮 2024-10-17 08:33:32

嗯,他们至少有一些共同的功能。 Seq[B] 继承自 Int =>; B(通过PartialFunction[Int, B]),Map[A, B]继承自A =>; B(也通过 PartialFunction[A, B]),并且 Set[A] 继承自 A =>布尔值。因此,就功能应用和组合方法而言,三者是可以互换使用的。此外,就遍历而言,它们可以互换使用,因为它们都实现了 TraversableLike

Well, they sort of do, at least actually common functionality. Seq[B] inherits from Int => B (via PartialFunction[Int, B]), Map[A, B] inherits from A => B (also via PartialFunction[A, B]), and Set[A] inherits from A => Boolean. Thus, as far as function application and composition methods are concerned, all three can be used interchangeably. Additionally, they can be used interchangeably as far as traversal goes, as all implement TraversableLike.

陌伤ぢ 2024-10-17 08:33:32

将序列视为从整数到元素的赋值只是描述序列的一种方式。还有其他方法,并且没有理由说明这种描述序列的方法应该成为规范。序列的实际目的是使一堆元素可访问和可遍历。实际为元素分配整数并不需要序列。例如,大多数 Stream 实现可能没有与遍历并行运行的计数器。要求这样做会给实施带来不必要的开销。

此外,Map[K,V]也是一个Iterable[(K,V)]。根据您的建议,Seq[A] 也必须是 Map[Int,A],这也使其成为 Iterable[( Int,A)]。由于 Seq 扩展了 Iterable,这将使 Seq[A] 既是一个 Iterable[A] 又是一个 < code>Iterable[(Int,A)] (以及递归地,一个 Iterable[(Int,(Int,A))]Iterable[(Int,(Int ,(Int,A)))] 等),这不是 Scala 中允许的继承方式。

您可以为您关于 Set 的建议构造一个类似的论点。

Seeing a sequence as an assignment from integers to elements is only one way to describe what a sequence is. There are other ways, and there is no reason why that way of describing a sequence should become canonical. The actual purpose of a sequence is to make a bunch of elements accessible and traversable. A sequence is not required to actually assign integer numbers to the elements. For example, most Stream implementations probably don't have a counter running in parallel to the traversal. Requiring that would impose an unnecessary overhead on the implementation.

Besides, a Map[K,V] is also an Iterable[(K,V)]. Following your suggestion, a Seq[A] would also have to be a Map[Int,A], which would by that also make it an Iterable[(Int,A)]. Since Seq extends Iterable, this would make the Seq[A] both an Iterable[A] and an Iterable[(Int,A)] (and, recursively, an Iterable[(Int,(Int,A))], Iterable[(Int,(Int,(Int,A)))], and so on), which is not an allowed way of inheritance in Scala.

You can construct a similar argument for your suggestion regarding Set.

小伙你站住 2024-10-17 08:33:32

好吧,如果您所关心的 SeqSet 就是这些,那么您就说对了。我自己碰巧认为这是最不重要的方面之一,并且所有这些方面都已经很好地体现为函数

也就是说,Map 是将键转换为值的函数,Seq 是将 Int 转换为值的函数,而Set 是将值转换为Boolean 的函数。这个属性,你称之为“地图”,是一个函数。而且它已经被三人共享。

在我看来,MapSeqSet 的真正含义是:

  • A Seq 是关心了解其元素的顺序。从概念上讲,如何在 Map 中添加元素?您必须对所有键重新编号!

  • Set 关注元素是否存在。如何在 Map 中对其进行建模?它必须是一张具有默认值的地图——而不是一张普通的地图——并且其中所有非默认值都是相同的!这显然是一种退化行为,而不是抽象。

  • Map 关注将任意键映射到任意值。 Seq 没有任意键,Set 没有任意值。

Well, if all you care about Seq and Set was that, you'd have a point. Myself, I happen to think that's one of the least importants aspects, and one which is already well represented by all of them being functions.

That is, a Map is a function of a key into a value, a Seq is a function of an Int into a value, and a Set is a function of a value into a Boolean. This property, which you called a "map", is a funciton. And it is already shared by all three.

What, in my opinion, Map, Seq and Set are really about are:

  • A Seq is concerned about knowing in what order its elements are. Conceptually, how would you prepend an element in a Map? You'd have to renumber all keys!

  • A Set is concerned about the presence or absence of an element. How one would model that in a Map? It would have to be a map with default value -- not a common map -- and one in which all non-default values are the same! That is clearly a degenerate behavior, not an abstraction.

  • A Map is concerned about mapping arbitrary keys to arbitrary values. A Seq doesn't have arbitrary keys, and a Set doesn't have arbitrary values.

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