当任何元素比头元素长时 List.transpose 崩溃
如图所示:
scala> List(List(1, 2), List(3, 4), List(5, 6)) transpose
res7: List[List[Int]] = List(List(1, 3, 5), List(2, 4, 6))
scala> List(List(1, 2), List(3), List(5, 6)) transpose
res8: List[List[Int]] = List(List(1, 3, 5), List(2, 6))
scala> List(List(1, 2), List(3, 4, 7), List(5, 6)) transpose
java.lang.IndexOutOfBoundsException: 2
at scala.collection.immutable.Vector.checkRangeConvert(Vector.scala:104)
...
这种行为是故意的吗?如果是这样,为什么?
编辑:尽管问题的一部分已经得到澄清,我仍然想提出一个接受不规则尺寸的方法版本(可能使用不同的名称)。
As illustrated:
scala> List(List(1, 2), List(3, 4), List(5, 6)) transpose
res7: List[List[Int]] = List(List(1, 3, 5), List(2, 4, 6))
scala> List(List(1, 2), List(3), List(5, 6)) transpose
res8: List[List[Int]] = List(List(1, 3, 5), List(2, 6))
scala> List(List(1, 2), List(3, 4, 7), List(5, 6)) transpose
java.lang.IndexOutOfBoundsException: 2
at scala.collection.immutable.Vector.checkRangeConvert(Vector.scala:104)
...
Is this behaviour deliberate? If so, why?
EDIT: even though part of the question has been clarified, I'd still like to propose a version of this method (perhaps with a different name) that accepts irregular sizes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是故意的。任何使用不规则尺寸的尝试都会很快失败。查看错误报告 https://issues.scala-lang.org/browse/SI -3399。另请查看如何为您的案例编写转置:Scala 中是否有一种安全的方法来转置不等长列表的列表?
它在旧方法中工作。其中 7 被默默地忽略,而在前面的示例中,所有数字都出现在结果中。我想这是不可取的:
Yes it's intentional. It fails fast to any attempt to use irregular sizes. Check out the bug report https://issues.scala-lang.org/browse/SI-3399. Also take a look at how to write a transpose for your case: Is there a safe way in Scala to transpose a List of unequal-length Lists?
It works in the old method. Where 7 is silently ignored whereas in the previous examples all numbers appeared the result. I guess this was undesirable: