Yet Another Perl 6 Operator: Zip

by Adriano Ferreira


Perl 6 has an operator Z, named zip, to interleave elements of two or more arrays.



my @a = 1,2 Z -1,-2; # (1,-1),(2,-2)



The zip is one of the list generating operators that gives the language some flavor of functional programming. This gets further as the usual semantics for lists is to get lazy generation, which means easy/efficient handling of large lists (and also the extreme case of infinite lists).


2 Comments

nsome
2007-09-19 08:51:00
Very, very strange feature. In perl 5 I can use
use List::Util;
...
map [ $a[$_], $b[$_] ], 0..max(scalar @a, scalar @b)-1


for same result. On the other hand zipping is not so frequently operation... Very strange feature.

Adriano Ferreira
2007-09-19 11:30:26
In Perl 5 you can use &zip from List::MoreUtils as well and the related iterators: &pairwise, &each_array, &each_arrayref. In Perl 6, the zip operator is enough to get all the behavior of these. You said the operation is not frequent, but I think it is so due to the lack of built-in support for it. Time will tell.