I was surprised by the lack of such a function in List::MoreUtils recently. There is natatime, but it returns an iterator rather than using a callback.

I wonder how much speed you'd gain by replacing $f->($a,$b) with &$f. Another advanced feature for the collection!

It would be a nice bonus if you $a and $b were aliases to the args like with map.

sub map_pairs(&@) { my $cb = shift; my $caller = caller(); my $ap = do { no strict 'refs'; \*{$caller.'::a'} }; my $bp = do { no strict 'refs'; \*{$caller.'::b'} }; local *$ap; local *$bp; my @res; while (@_) { *$ap = \shift; *$bp = \shift; push @res, &$cb; } return @res; }

In reply to Re^2: Mini-Tutorial: Working with Odd/Even Elements by ikegami
in thread Mini-Tutorial: Working with Odd/Even Elements by ikegami

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":