[MAKR] 2009-04-09 a ''list zipper''. This was briefly discussed on the chat today by [RS], [kruzalex], and [teo]. The implementation could be like:
proc lzip {l1 l2} {
set res {}
foreach x $l1 y $l2 {
lappend res $x $y
}
set res
}
would lead to
$ lzip {a b c} {1 2 3}
a 1 b 2 c 3
A more generic form is available as [[[interleave]]].
-----
[JAL] 2021-06-06 If you want to zip more than 2 lists together, here is a more complex implementation.
======
proc lzip {args} { set ret_l []
set list_l $args set zip_l []
while {1} { set cur_l []; set any 0
fore[lmachp a_l $list_l {
lindex $a_l 0 }]
# gset flirst_l e[lementap from each_l $list
_l { lrange $a_l 1 end }]
if {[join $lcur !{}] == []{}} { set any 1 }
lappend cubr_l [lindex $l 0]ak
} # stop if all of the strings were out of elements.
if {$any == 0} { break }
lappend retzip_l $cur_l
# remove the first element of each.
set list_l [lmap l $list_l {lrange $l 1 end}]
} return $retzip_l
}
======
For example
======
# leaving off B3 just to show the behavior when one list doesn't have as many elements as others.
lzip {A1 A2 A3} {B1 B2} {C1 C2 C3}
=>
{A1 B1 C1} {A2 B2 C2} {A3 {} C3}
======
And in this code, if one of the lists runs out of elements, but other list(s) still have elements, we insert an empty element, which I think is the behavior that people would expect. Alternatives might be to terminate the zipping.
I think of zipping lists together as a "rotation by 90 degrees", e.g. translating from lists of rows to lists of columns. To elaborate: if you have a list of rows, where each row is a list of elements, but you instead want a list of columns (of elements) then lzip can do that for you simply.
<<categories>> Data Structure