File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 487487 (set _index (+ 1 _index)) })
488488 _c }))
489489
490+ # @brief Zip two lists into one, using a filler is one list is shorter: [1 2 3] and [5 6 7 8] will give [[1 5] [2 6] [3 7] [0 8]]
491+ # @param _a the first list to work on
492+ # @param _b the second list to work on
493+ # @param _filler value to use if there is not enough items in one of the lists
494+ # @details The original lists are not modified.
495+ # =begin
496+ # (let a [1 2 3])
497+ # (let b [5 6 7 8])
498+ # (print (list:zipLongest a b 0)) # [[1 5] [2 6] [3 7] [0 8]]
499+ # =end
500+ # @author https://github.com/SuperFola
501+ (let zipLongest (fun ((ref _a) (ref _b) _filler) {
502+ (let _m (math:max (len _a) (len _b)))
503+ (mut _c [])
504+ (mut _index 0)
505+
506+ (while (< _index _m) {
507+ (append! _c [
508+ (if (< _index (len _a)) (@ _a _index) _filler)
509+ (if (< _index (len _b)) (@ _b _index) _filler)])
510+ (set _index (+ 1 _index)) })
511+ _c }))
512+
490513# @brief Zip a list elements with their index. [5 6 7 8] will give [[0 5] [1 6] [2 7] [3 8]]
491514# @param _L the list to iterate over
492515# @details The original list is not modified.
Original file line number Diff line number Diff line change 133133 (test:eq (list:zip a b) [[1 4] [2 5] [3 6]])
134134 (test:eq (list:zip [] []) []) })
135135
136+ (test:case "zipLongest" {
137+ (test:eq (list:zipLongest [1 2] b nil) [[1 4] [2 5] [nil 6]])
138+ (test:eq (list:zipLongest a [4 5] nil) [[1 4] [2 5] [3 nil]])
139+ (test:eq (list:zipLongest [] [] nil) []) })
140+
136141 (test:case "foldLeft" {
137142 (test:eq (list:foldLeft [] 0 (fun (x y) (+ x y))) 0)
138143 (test:eq (list:foldLeft ["1" "2" "3"] "" (fun (x y) (+ x y))) "123")
You can’t perform that action at this time.
0 commit comments