|
478 | 478 | (set _index (+ 1 _index)) }) |
479 | 479 | [_list1 _list2] })) |
480 | 480 |
|
| 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 | + |
481 | 506 | # @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]] |
482 | 507 | # @details Lists must have the same size. Otherwise, only the first (min (len _a) (len _b)) elements will be used. |
483 | 508 | # @param _a the first list to work on |
|
765 | 790 |
|
766 | 791 | _early })) |
767 | 792 |
|
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 | | - |
793 | 793 | # @brief Get the unique values in a given list |
794 | 794 | # @details The original list is not modified. |
795 | 795 | # @param _L list to extract unique values from |
|
0 commit comments