Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper exception for JaggedArray.transpose and JaggedList.transpose when inner arrays/lists have different lengths #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/FSharpAux/JaggedArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module JaggedArray =

/// Transposes a jagged array.
let transpose (arr: 'T [][]) =
if arr |> Array.exists (fun t -> t.Length <> arr[0].Length) then
failwith "Elements in input array have different lengths."
if arr.Length > 0 then
let colSize = arr.[0].Length
Array.init (colSize) (fun rowI -> Array.init (arr.Length) (fun colI -> (arr.[colI].[rowI])))
Expand Down Expand Up @@ -226,6 +228,8 @@ module JaggedList =

// Transposes a jagged list.
let transpose (data: 'T list list) =
if data |> List.exists (fun t -> t.Length <> data.Head.Length) then
failwith "Elements in input list have different lengths."
let rec transpose = function
| (_::_)::_ as M -> List.map List.head M :: transpose (List.map List.tail M)
| _ -> []
Expand Down
23 changes: 23 additions & 0 deletions tests/FSharpAux.Tests/JaggedArrayTest.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ let testJaggArr1_toIndexedArray_Equal =
[|(0, 0, "I "); (0, 1, "am "); (0, 2, "Jesus!"); (1, 0, "No"); (1, 1, " you"); (1, 2, "are"); (1, 3, "not")|]
let testJaggArr1_toIndexedArray_NotEqual =
[|(1, 0, "No"); (1, 1, " you"); (1, 2, "are"); (1, 3, "not"); (0, 0, "I "); (0, 1, "am "); (0, 2, "Jesus!")|]
let testJaggArr2 = [|[|1;2;3|]; [|4;5;6|]|]
let testJaggArr2_transposed = [|[|1;4|]; [|2;5|]; [|3;6|]|]
let testJaggArr3 = [|[|1;2;3|]; [|4;5|]|]


[<Tests>]
let jaggedArrayTests =
Expand Down Expand Up @@ -60,6 +64,13 @@ let jaggedArrayTests =
Expect.notEqual (JaggedArray.init 3 7 (fun i j -> i * 2, j * 3)) testJaggArr1_init_NotEqual "JaggedArray.init did not return incorrect jagged array"
)
]
testList "JaggedArray.transpose" [
testCase "returns correct jagged array" (fun _ ->
Expect.equal (JaggedArray.transpose testJaggArr2) testJaggArr2_transposed "JaggedArray.transpose did return incorrect jagged array"
)
testCase "throws exception when lengths are unequal" <| fun _ ->
Expect.throws (fun _ -> JaggedArray.transpose testJaggArr3 |> ignore) "JaggedArray.transpose did not throw when lengths are unequal"
]
]


Expand All @@ -73,6 +84,10 @@ let testJaggList1_init_NotEqual = [
[(0, 0); (0, 3); (0, 6); (0, 9); (0, 12); (0, 15); (0, 18)];
[(4, 0); (4, 3); (4, 6); (4, 9); (4, 12); (4, 15); (4, 18)]
]
let testJaggList2 = [[1;2;3]; [4;5;6]]
let testJaggList2_transposed = [[1;4]; [2;5]; [3;6]]
let testJaggList3 = [[1;2;3]; [4;5]]


[<Tests>]
let jaggedListTests =
Expand All @@ -85,4 +100,12 @@ let jaggedListTests =
Expect.notEqual (JaggedList.init 3 7 (fun i j -> i * 2, j * 3)) testJaggList1_init_NotEqual "JaggedList.init did not return incorrect list"
)
]
testList "JaggedList.transpose" [
testCase "returns correct jagged list" (fun _ ->
Expect.equal (JaggedList.transpose testJaggList2) testJaggList2_transposed "JaggedList.transpose did not return correct list"
)
testCase "throws exception when lengths are unequal" (fun _ ->
Expect.throws (fun _ -> JaggedList.transpose testJaggList3 |> ignore) "JaggedList.transpose did not throw when lengths are unequal"
)
]
]