为什么 Scala 列表没有排序?
Scala 中的列表没有隐式排序是否有原因?
val lists = List(List(2, 3, 1), List(2, 1, 3))
lists.sorted
error: could not find implicit value for parameter ord: Ordering[List[Int]]
编辑
是的,我的问题是为什么没有已经隐含在范围内的内置排序。对我来说,显然第二个列表应该“小于”第一个列表,因为 0 处的项目相等,而第二个列表的 1 处的项目较低。我想知道是否可能没有好的答案,当列表有两种不同的大小。
Is there a reason why there is no implicit Ordering for Lists in Scala?
val lists = List(List(2, 3, 1), List(2, 1, 3))
lists.sorted
error: could not find implicit value for parameter ord: Ordering[List[Int]]
EDIT
Yes, my question is why there is no built-in Ordering that's already implicitly in scope. To me, it seems obvious that the second list should be "less than" the first list since the items at 0 are equal and the second list has the lower item at 1. I was wondering if maybe it's that there's no good answer when the Lists are two different sizes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为这是一个疏忽。字典顺序对 Seq 确实有意义。我们应该将它添加到标准库中。
I think it's an oversight. Lexicographic ordering does make sense on Seqs. We should add it to the standard library.
顺便说一句,即使在我修复此问题之前,您也可以通过其他方式执行此操作:
但现在它的工作原理就像您希望的那样。
编辑:由于必要隐含的粗略分歧问题,我将其移出了默认范围。具有像这样跨越界限的隐式转换:
...是潜在的问题根源。它将在 2.9 中可用,但您必须按如下方式导入它。
Incidentally even before I fixed this you could do this other ways:
But now it works like you'd hope.
Edit: due to sketchy divergence issues with the requisite implicit I moved it out of the default scope. Having an implicit conversion which acts across a bound like this:
...is a potential recipe for issues. It'll be available in 2.9, but you have to import it as follows.
您拥有的是列表的列表,而不是整数的列表。您缺少的是确定列表是否<=另一个列表的标准。
这就是错误消息的内容:我找不到将列表与另一个列表进行比较的方法,您应该明确提供一个。
如果您的问题是“为什么列表没有与其他列表的内置比较方法”,那么,事情就是这样。
What you have is a list of lists, not a list of integers. What you are missing is a criteria for determining whether a list is <= another list, or not.
That's what the error message says: I can't find a way to compare a list to another list, you should provide one explicitly.
If your question was "why don't list have a built-in comparison method against other lists", well, that's just the way it is.
List[Int] 类上唯一真正合理的总顺序是字典顺序(即比较列表的第一个元素,如果它们相等则比较第二个元素,如果秒相等则比较第三个元素,等等)。标准库没有提供这一点,可能是因为实际需要它的情况并不多。创建从 List[X] 到 Ordering[List[X]] 的隐式转换来实现它是很容易的,然后您可以在需要的地方简单地导入该转换。
The only really sensible total order over the class of List[Int] would be lexicographic (i.e, compare the first elements of the list, then the second if they are equal, the third if the seconds are equal, etc.). This isn't provided by the standard library, probably because there aren't that many cases where it's actually needed. It would be easy enough to create an implicit conversion from List[X] to Ordering[List[X]] that would implement that, and then you could simply import that conversion wherever you needed it.
您可以使用 sortWith。这不会考虑不同大小的列表,因为 zip 会消除差异,但我认为它的作用类似于您所追求的:
You can use sortWith. This doesn't take differently sized lists into account because zip will throw out the difference, but I think it does something like what you're after:
Scala 2.13.3 - 调用
sorted
方法对我有用:sorted
方法是 StrictOptimizedSeqOps 特征的成员,:Scala 2.13.3 - invoking
sorted
method worked for me:sorted
method is a member of StrictOptimizedSeqOps trait,:在较新的 Scala 版本中(使用 2.12.5 进行测试)有一个 对 Iterable[A] 进行排序。只需将正确的类型赋予变量
lists
:或者将元素转换为
Iterable[]
的实例(这对于List[] 实例来说是无操作的) ):
In newer Scala versions (tested with 2.12.5) there's an Ordering for Iterable[A]. Just ascribe the right type to your variable
lists
:Or convert the elements to instances of
Iterable[]
(which is a no-op for instances ofList[]
):