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" <|