Graham Barr wrote:
> > > But then went onto interators and something like
> > >
> > > @list = interleave(@a,@b,@c);
> > >
> > > which would interleave the lists given, and
> > >
> > > foreach ($a,$b,$c) (interleave(@a,@b,@c))
sub interleave(\@;\@\@\@\@\@\@\@\@) {
my $m = -1;
for ( @_ ) { $m < $#{$_} and $m = $#{$_} }
map { my $i = $_; [ map { $_->[$i] } @_ ] } 0..$m;
}
for ( map { @$_ } interleave( @a, @b, @c ) )
> Gisle Aas wrote:
> > Python also has a map function that can take multiple lists and iterate
> > over them in sync. E.g.
> >
> > map(func, list1, list2, list3)
> >
> > where 'func' must be a function taking 3 arguments. Can't see how to
> > easily extend perl's map in that way. Perhaps we could introduce
> > map2, map3,... builtins? :-)
sub mapf(&;\@\@\@\@\@\@\@\@\@) {
my $cr = shift;
my $m = -1;
for ( @_ ) { $m < $#{$_} and $m = $#{$_} }
map { my $i = $_; $cr->( map { $_->[$i] } @_ ) } 0..$m;
}
I guess my question is, why do these need to be builtins?
There is no limit to the funky algorithms one can come up with;
not everyting should go in the core.
--
John Porter
Aus tiefem Traum bin ich erwacht.