[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Iternating over multiple lists



2000-04-11-11:08:49 Bernie Cosell:
> What perl needed to make the solution to this problem trivial (and
> elegant) is "multiple map" (or, if you rather "parallel map").
> That is, imagine if you could do:
>
>    mmap {BLOCK} list1 list2 list3 list4 ....
>
> and BLOCK would be evaluated with $1, $2, $3, $4, ... all bound to
> corresponding elements from each of the lists.  [Yes, I know you
> can't pass lists around that way... just kicking around an idea..
> maybe you'd have to do it with a list-of-references-to-lists or
> some such]

And $1, $2, ... are also taken, I'd be a bit creeped out trying to
reuse them that way. But how about $_[0], $_[1], ...?

In which case, the immediately implementable syntax would be
something like

	multimap sub { ... } [list1], \@array2, ...?

(mmap suggests memory mapping to me, I'd dodge that name).

sub multimap {
	my $subr = shift;
	my @lists = @_;
	my $max = 0;
	for (@lists) {
		$max = $#{$_} if $#{$_} > $max;
	}
	for my $i (0 .. $max) {
		&$subr(map { $lists[$_][$i] } 0 .. $#lists);
	}
}

or thereabouts.

-Bennett

PGP signature