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

Re: [FWP] Iternating over multiple lists



>   mmap {BLOCK} list1 list2 list3 list4 ....

That could be implemented in this fashion:

  print mmap { "$1 $2\n" } \@foo, \@bar;
  @sum = mmap { $1 + $2 + $3 } [ alpha() ], [ beta() ], [ gamma() ];

  sub mmap (&@) {
    my $cref = shift;
    my @return;
    no strict 'refs';  # we're about to do the nasty

    eval
      "local (" .
      join(",", map "*$_", 1 .. @_)
      . ")";

    for my $iter (0 .. $#{$_[0]}) {  # only go as long as the first list
      my $i = 1;
      for (@_) { *{$i++} = \( $_[$_][$iter] );
      wantarray ? push(@return, &$cref) : &$cref;  # or $cref->()
    }

    return @return if wantarray;
  }

Roast me, toast me, burn me to a crisp. :)

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy@pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe