From 8868d00981ed87ac5d4c926c2a200cd08db32b4a Mon Sep 17 00:00:00 2001 From: Rogerio Chaves Date: Sun, 24 Sep 2017 20:57:04 -0300 Subject: [PATCH] make transpose call stack size safe --- src/List/Extra.elm | 13 +++++++++---- tests/Tests.elm | 5 +++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/List/Extra.elm b/src/List/Extra.elm index 4cdfaa2..4c07c17 100644 --- a/src/List/Extra.elm +++ b/src/List/Extra.elm @@ -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 @@ -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. diff --git a/tests/Tests.elm b/tests/Tests.elm index caa134f..921e864 100644 --- a/tests/Tests.elm +++ b/tests/Tests.elm @@ -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" <|