Skip to content

Commit

Permalink
make transpose call stack size safe
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriochaves committed Sep 24, 2017
1 parent 153f40c commit 8868d00
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,18 @@ If some rows are shorter than the following rows, their elements are skipped:
-}
transpose : List (List a) -> List (List a)
transpose ll =
transpose =
transposeHelp []


transposeHelp : List (List a) -> List (List a) -> List (List a)
transposeHelp acc ll =
case ll of
[] ->
[]
acc

[] :: xss ->
transpose xss
transposeHelp acc xss

(x :: xs) :: xss ->
let
Expand All @@ -791,7 +796,7 @@ transpose ll =
tails =
filterMap tail xss
in
(x :: heads) :: transpose (xs :: tails)
transposeHelp (acc ++ [ x :: heads ]) (xs :: tails)


{-| Return the list of all subsequences of a list.
Expand Down
5 changes: 5 additions & 0 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ all =
Expect.equal
(transpose [ [ 10, 11 ], [ 20 ], [], [ 30, 31, 32 ] ])
[ [ 10, 20, 30 ], [ 11, 31 ], [ 32 ] ]
, 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 8868d00

Please sign in to comment.