0

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.

Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59

1 Answers1

3

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:

scala> List.transpose(List(List(1, 2), List(3, 4, 7), List(5, 6)))
warning: there were 1 deprecation warnings; re-run with -deprecation for details
res4: List[List[Int]] = List(List(1, 3, 5), List(2, 4, 6))
Community
  • 1
  • 1
Rustem Suniev
  • 1,149
  • 9
  • 8
  • No, it only fails if the first element is shorter than any other, i.e. `List(List(1, 2, 3), List(4), List(5, 6)) transpose` works well. This is inconsistent behaviour in my opinion, i.e. why should the first element be special? – Knut Arne Vedaa Feb 19 '11 at 16:47
  • From the transpose doc: Transposes a list of lists. Pre: All element lists have the same length. But I agree it should fail in the second case as well. – Rustem Suniev Feb 19 '11 at 17:00