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

Make transpose call stack size safe #83

Merged
merged 2 commits into from
Dec 18, 2017
Merged
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
23 changes: 9 additions & 14 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -813,23 +813,18 @@ If some rows are shorter than the following rows, their elements are skipped:
-}
transpose : List (List a) -> List (List a)
transpose ll =
case ll of
[] ->
[]
transpose listOfLists =
List.foldr (List.map2 (::)) (List.repeat (rowsLength listOfLists) []) listOfLists

[] :: xss ->
transpose xss

(x :: xs) :: xss ->
let
heads =
filterMap head xss
rowsLength : List (List a) -> Int
rowsLength listOfLists =
case listOfLists of
[] ->
0

tails =
filterMap tail xss
in
(x :: heads) :: transpose (xs :: tails)
x :: _ ->
List.length x


{-| Return the list of all subsequences of a list.
Expand Down
11 changes: 8 additions & 3 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ all =
Expect.equal
(transpose [ [ 1, 2, 3 ], [ 4, 5, 6 ] ])
[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
, test "short rows are skipped" <|
, test "truncate the matrix to the shortest row size" <|
\() ->
Expect.equal
(transpose [ [ 10, 11 ], [ 20 ], [], [ 30, 31, 32 ] ])
[ [ 10, 20, 30 ], [ 11, 31 ], [ 32 ] ]
(transpose [ [ 10, 11 ], [ 20 ], [ 30, 31, 32 ] ])
[ [ 10, 20, 30 ] ]
, test "transposes large lists" <|
\() ->
Expect.equal
(transpose [ List.repeat 10000 1 ])
(List.repeat 10000 [ 1 ])
]
, describe "subsequences" <|
[ test "computes subsequences" <|
Expand Down