Skip to content

Commit 6af64a7

Browse files
PatrickMcDonaldTIHan
authored andcommitted
List.transpose should throw error when given jagged array (#6908) (#6989)
* List.transpose should throw error when given jagged array (#6908) * transpose does not throw when one of the elements is empty * Add additional test cases
1 parent c3b6dff commit 6af64a7

File tree

2 files changed

+12
-6
lines changed
  • src/fsharp/FSharp.Core
  • tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections

2 files changed

+12
-6
lines changed

src/fsharp/FSharp.Core/local.fs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,25 +717,28 @@ module internal List =
717717
invalidArgDifferentListLength "list.[0]" (System.String.Format("list.[{0}]", j)) t.Length
718718
[], [], 0
719719
| h :: t ->
720+
let mutable j = 0
721+
for t' in tail do
722+
j <- j + 1
723+
if t'.IsEmpty then
724+
invalidArgDifferentListLength (System.String.Format("list.[{0}]", j)) "list.[0]" (t.Length + 1)
720725
let headsCons = freshConsNoTail h
721726
let tailsCons = freshConsNoTail t
722727
let headCount = transposeGetHeadsFreshConsTail headsCons tailsCons tail 1
723728
headsCons, tailsCons, headCount
724729

725730
/// Append the next element to the transposed list
726-
let rec transposeToFreshConsTail cons list expectedCount =
731+
let rec transposeToFreshConsTail cons list =
727732
match list with
728733
| [] -> setFreshConsTail cons []
729734
| _ ->
730735
match transposeGetHeads list with
731736
| [], _, _ ->
732737
setFreshConsTail cons []
733-
| heads, tails, headCount ->
734-
if headCount < expectedCount then
735-
invalidArgDifferentListLength (System.String.Format("list.[{0}]", headCount)) "list.[0]" <| tails.[0].Length + 1
738+
| heads, tails, _ ->
736739
let cons2 = freshConsNoTail heads
737740
setFreshConsTail cons cons2
738-
transposeToFreshConsTail cons2 tails expectedCount
741+
transposeToFreshConsTail cons2 tails
739742

740743
/// Build the transposed list
741744
let transpose (list: 'T list list) =
@@ -746,7 +749,7 @@ module internal List =
746749
let heads, tails, headCount = transposeGetHeads list
747750
if headCount = 0 then [] else
748751
let cons = freshConsNoTail heads
749-
transposeToFreshConsTail cons tails headCount
752+
transposeToFreshConsTail cons tails
750753
cons
751754

752755
let rec truncateToFreshConsTail cons count list =

tests/FSharp.Core.UnitTests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,9 @@ type ListModule02() =
842842
// jagged lists
843843
CheckThrowsArgumentException (fun () -> List.transpose [[1; 2]; [3]] |> ignore)
844844
CheckThrowsArgumentException (fun () -> List.transpose [[1]; [2; 3]] |> ignore)
845+
CheckThrowsArgumentException (fun () -> List.transpose [[]; [1; 2]; [3; 4]] |> ignore)
846+
CheckThrowsArgumentException (fun () -> List.transpose [[1; 2]; []; [3; 4]] |> ignore)
847+
CheckThrowsArgumentException (fun () -> List.transpose [[1; 2]; [3; 4]; []] |> ignore)
845848

846849
[<Test>]
847850
member this.Truncate() =

0 commit comments

Comments
 (0)