Skip to content

Commit

Permalink
use @pzp1997's faster implementation of transpose
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriochaves committed Nov 29, 2017
1 parent a1bc604 commit 017e824
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
26 changes: 8 additions & 18 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -813,28 +813,18 @@ If some rows are shorter than the following rows, their elements are skipped:
-}
transpose : List (List a) -> List (List a)
transpose =
transposeHelp []
transpose listOfLists =
List.foldr (List.map2 (::)) (List.repeat (rowsLength listOfLists) []) listOfLists


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

[] :: xss ->
transposeHelp acc xss
0

(x :: xs) :: xss ->
let
heads =
filterMap head xss

tails =
filterMap tail xss
in
transposeHelp (acc ++ [ x :: heads ]) (xs :: tails)
x :: _ ->
List.length x


{-| Return the list of all subsequences of a list.
Expand Down
6 changes: 3 additions & 3 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ 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
Expand Down

0 comments on commit 017e824

Please sign in to comment.