Skip to content

Commit ab46339

Browse files
committed
fix(list): transpose should use the smallest element size as the limit
1 parent 5f9c9be commit ab46339

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

List.ark

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,31 @@
478478
(set _index (+ 1 _index)) })
479479
[_list1 _list2] }))
480480

481+
# @brief Transpose a list of lists or list of strings
482+
# @details The original list is not modified. The smallest element size is used as the limit for selecting values
483+
# @param _L list of lists/strings to transpose
484+
# =begin
485+
# (let data [[1 2 3] [4 5 6] [7 8 9])
486+
# (print (list:transpose data)) # [[1 4 7] [2 5 8] [3 6 9]]
487+
# =end
488+
# @author https://github.com/SuperFola
489+
(let transpose (fun (_L) {
490+
(mut _output [])
491+
(mut _i 0)
492+
(let _width (min (map _L len)))
493+
(let _height (len _L))
494+
495+
(while (< _i _width) {
496+
(mut _column [])
497+
(mut _k 0)
498+
(while (< _k _height) {
499+
(append! _column (@@ _L _k _i))
500+
(set _k (+ _k 1)) })
501+
502+
(append! _output _column)
503+
(set _i (+ 1 _i)) })
504+
_output }))
505+
481506
# @brief Zip two lists into one: [1 2 3 4] and [5 6 7 8] will give [[1 5] [2 6] [3 7] [4 8]]
482507
# @details Lists must have the same size. Otherwise, only the first (min (len _a) (len _b)) elements will be used.
483508
# @param _a the first list to work on
@@ -765,31 +790,6 @@
765790

766791
_early }))
767792

768-
# @brief Transpose a list of lists or list of strings
769-
# @details The original list is not modified. Elements should have the same length
770-
# @param _L list of lists/strings to transpose
771-
# =begin
772-
# (let data [[1 2 3] [4 5 6] [7 8 9])
773-
# (print (list:transpose data)) # [[1 4 7] [2 5 8] [3 6 9]]
774-
# =end
775-
# @author https://github.com/SuperFola
776-
(let transpose (fun (_L) {
777-
(mut _output [])
778-
(mut _i 0)
779-
(let _width (len (head _L)))
780-
(let _height (len _L))
781-
782-
(while (< _i _width) {
783-
(mut _column [])
784-
(mut _k 0)
785-
(while (< _k _height) {
786-
(append! _column (@@ _L _k _i))
787-
(set _k (+ _k 1)) })
788-
789-
(append! _output _column)
790-
(set _i (+ 1 _i)) })
791-
_output }))
792-
793793
# @brief Get the unique values in a given list
794794
# @details The original list is not modified.
795795
# @param _L list to extract unique values from

tests/list-tests.ark

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@
219219
(test:eq (list:transpose data) [["1" " "] ["2" "4"] ["3" "5"] [" " " "] ["3" "6"] ["2" "4"] ["8" " "] [" " " "] [" " "3"] ["5" "8"] ["1" "7"]])
220220
(test:eq (list:transpose (list:transpose zipped)) zipped)
221221
(test:eq (list:transpose [[]]) [])
222-
(test:eq (list:transpose [[1]]) [[1]]) })
222+
(test:eq (list:transpose [[1]]) [[1]])
223+
(test:eq (list:transpose [[1 2] [4 5 6]]) [[1 4] [2 5]])
224+
(test:eq (list:transpose [[1 2 3] [4 5]]) [[1 4] [2 5]]) })
223225

224226
(test:case "unique" {
225227
(test:eq (list:unique []) [])

0 commit comments

Comments
 (0)