Scala 中什么时候一个 Set 小于另一个 Set?

发布于 2024-09-08 12:35:27 字数 507 浏览 4 评论 0原文

我想比较 Scala 中两个集合的基数。由于某些东西有时在 Scala 中“正常工作”,因此我尝试在集合之间使用 < 。它似乎通过了,但我无法从结果中看出任何意义。

示例:

scala> Set(1,2,3) < Set(1,4)
res20: Boolean = true
  • 它返回什么?
  • 在 API 中哪里可以阅读有关此方法的信息?
  • 为什么它没有列在 scala.collection.immutable.Set 下的任何地方?

更新:甚至集合中元素的顺序(??)似乎也很重要:

scala> Set(2,3,1) < Set(1,3)
res24: Boolean = false

scala> Set(1,2,3) < Set(1,3)
res25: Boolean = true

I wanted to compare the cardinality of two sets in Scala. Since stuff sometimes "just work" in Scala, I tried using < between the sets. It seems to go through, but I can't make any sense out of the result.

Example:

scala> Set(1,2,3) < Set(1,4)
res20: Boolean = true
  • What does it return?
  • Where can I read about this method in the API?
  • Why isn't it listed anywhere under scala.collection.immutable.Set?

Update: Even the order(??) of the elements in the sets seem to matter:

scala> Set(2,3,1) < Set(1,3)
res24: Boolean = false

scala> Set(1,2,3) < Set(1,3)
res25: Boolean = true

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

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

发布评论

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

评论(3

空城仅有旧梦在 2024-09-15 12:35:28

这不适用于 2.8。在 Scala 2.7 上,发生的情况是这样的:

scala.Predef.iterable2ordered(Set(1, 2, 3): Iterable[Int]) < (Set(1, 3, 2): Iterable[Int])

换句话说,在 scala.Predef 上定义了一个隐式转换,它是从 Iterable[A]< 为所有 Scala 代码“导入”的。 /code> 到 Ordered[Iterable[A]],前提是存在隐式 A =>;已订购[A]可用。

鉴于集合的可迭代顺序未定义,您无法真正对其进行太多预测。例如,如果添加元素以使集合大小大于四,您将得到完全不同的结果。

This doesn't work with 2.8. On Scala 2.7, what happens is this:

scala.Predef.iterable2ordered(Set(1, 2, 3): Iterable[Int]) < (Set(1, 3, 2): Iterable[Int])

In other words, there's an implicit conversion defined on scala.Predef, which is "imported" for all Scala code, from an Iterable[A] to an Ordered[Iterable[A]], provided there's an implicit A => Ordered[A] available.

Given that the order of an iterable for sets is undefined, you can't really predict much about it. If you add elements to make the set size bigger than four, for instance, you'll get entirely different results.

我是男神闪亮亮 2024-09-15 12:35:28

如果你想比较基数,直接这样做:

scala> Set(1, 2, 3).size < Set(2, 3, 4, 5).size
res0: Boolean = true

If you want to compare the cardinality, just do so directly:

scala> Set(1, 2, 3).size < Set(2, 3, 4, 5).size
res0: Boolean = true
妥活 2024-09-15 12:35:28

我对 Scala 的了解并不广泛,但是做了一些测试,我得到以下结果:

scala> Set(1,2) <
<console>:5: error: missing arguments for method < in trait Ordered;
follow this method with `_' if you want to treat it as a partially applied function
   Set(1,2) <
            ^

这告​​诉我 < 来自特征 Ordered。更多提示:

scala> Set(1,2) < _
res4: (Iterable[Int]) => Boolean = <function>

也就是说,Set 被评估为 Iterable,因为可能存在从 Iterable[A] 到 Ordered[Iterable[A]] 的隐式转换,但是我不再确定......测试不一致。例如,这两个可能建议进行一种字典顺序比较:

scala> Set(1,2,3) < Set(1,2,4)
res5: Boolean = true

1 相等,2 相等,3 小于 4。

scala> Set(1,2,4) < Set(1,2,3)
res6: Boolean = false

但这些则不然:

scala> Set(2,1) < Set(2,4)
res11: Boolean = true

scala> Set(2,1) < Set(2,2)
res12: Boolean = false

我认为正确的答案是在 Ordered 特征正确:除了比较其 hashCode 之外,集合之间没有任何 < 实现:

Ordered[A] 实例的 hashCode 方法与比较方法保持一致非常重要。但是,不可能提供合理的默认实现。因此,如果您需要能够计算 Ordered[A] 实例的哈希值,您必须在继承或实例化时自己提供它。

My knowledge of Scala is not extensive, but doing some test, I get the following:

scala> Set(1,2) <
<console>:5: error: missing arguments for method < in trait Ordered;
follow this method with `_' if you want to treat it as a partially applied function
   Set(1,2) <
            ^

That tells me that < comes from the trait Ordered. More hints:

scala> Set(1,2) < _
res4: (Iterable[Int]) => Boolean = <function>

That is, the Set is evaluated into an Iterable, because maybe there is some implicit conversion from Iterable[A] to Ordered[Iterable[A]], but I'm not sure anymore... Tests are not consistent. For example, these two might suggest a kind of lexicographical compare:

scala> Set(1,2,3) < Set(1,2,4)
res5: Boolean = true

1 is equal, 2 is equal, 3 is less than 4.

scala> Set(1,2,4) < Set(1,2,3)
res6: Boolean = false

But these ones don't:

scala> Set(2,1) < Set(2,4)
res11: Boolean = true

scala> Set(2,1) < Set(2,2)
res12: Boolean = false

I think the correct answer is that found in the Ordered trait proper: There is no implementation for < between sets more than comparing their hashCode:

It is important that the hashCode method for an instance of Ordered[A] be consistent with the compare method. However, it is not possible to provide a sensible default implementation. Therefore, if you need to be able compute the hash of an instance of Ordered[A] you must provide it yourself either when inheiriting or instantiating.

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