Skip to content

Commit

Permalink
Merge pull request #83 from rogeriochaves/master
Browse files Browse the repository at this point in the history
Make transpose call stack size safe
  • Loading branch information
Chadtech committed Dec 18, 2017
2 parents 4570d3b + 017e824 commit ec6c312
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
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

0 comments on commit ec6c312

Please sign in to comment.